Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Incrementing every letter in a string that occupies an odd position.

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Incrementing every letter in a string that occupies an odd position.

    Hi, for an "encryption" project, i need your help creating a method that will be incrementing

    by 5 every char in a string that occupies an odd position in that same string.

    For example : 'adam' should return 'aiar'

    -Method can't use arrays.
    -Method can't use regular expressions like 'split' etc..in order words the teacher wants us

    to use the simplest stuff inside the String class.
    -Only letters must be modified.

    so far i have this : (names of values are in french, sorry)

    public class Main {
     
    public static String incrementerDeCinqImpaires (String chaine) {
     
     
    char j = chaine.charAt(1);
    char x = chaine.charAt(3);
    char o = chaine.charAt(5);
     
     
    j+=5;
    x+=5;
    o+=5;
     
     
    System.out.println(x);
    System.out.println(j);
    System.out.println(o);
    String   tmpString = chaine.replace( chaine.charAt(1), j );
    String   tmpString2 = tmpString.replace( tmpString.charAt(3), x );
    String   tmpString3 = tmpString2.replace( tmpString2.charAt(5), o );
    System.out.println( "Original = " + chaine );
    System.out.println( "Result   = " + tmpString3 );
     
     
     
    return tmpString3;
     
    }
    public static void main (String args [] ) {
     
     
           System.out.println (incrementerDeCinqImpaires("adam"));
     
     
     
        }
    }

    The thing is i need a "for" loop (which has been a nightmare for me trying to figure out how

    to make it) in order to increment every odd positioned char in the string.

    So all I need really is to sum up what i posted above inside a for loop (or more). Then just

    add ifs and elses so i can increment only letters.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Incrementing every letter in a string that occupies an odd position.


  3. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Incrementing every letter in a string that occupies an odd position.

    Darryl I don't think there's anything wrong with posting to seperate forums. I understand why there might be one with two sperate groups of people working towards the same answer but I'm not sure.

    Anyways to the question what you need is indeed a for loop and that for loop will need to run from the first letter in the string to the last letter and if it is odd it will need to do the increment
    For(....)
    {
        If(....)
        {
            .....(you could maybe have a method to handle this part)
         }
    }

    What I want you to do is try to write what I left out because I want you to figure this out for yourself in the main. So have a stab at that or at least write your thoughts of how it should work. Also if this has already been solved in the other forums change the title to show that.

    You'll want to take a look at this

    String (Java 2 Platform SE 5.0)

    Also to replace individual characters you may have to use a StringBuilder instead I'm not sure, so check this out also

    StringBuilder (Java 2 Platform SE 5.0)

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Incrementing every letter in a string that occupies an odd position.

    Quote Originally Posted by Faz View Post
    Darryl I don't think there's anything wrong with posting to seperate forums.
    And where did I say it's wrong, that you felt the need to point that out to me?

    db

  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Incrementing every letter in a string that occupies an odd position.

    Just the impression I got. My apologies so.

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  3. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM
  4. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM
  5. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 10:33 AM