Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default increment letters from a string

    Hi all, i need help with this code im trying to produce. I've been searching google for days without finding any answers so please youve got to help me understand. The idea is to encrypt a string inputted by the user

    I wish to increment the letters at odd positions by 5 in a string (only the letters..the other characters must stay the same)

    for example : 2345f57hBFE&&/f3 should result in 2345f57mBKE&&/f3

    example : 'h' which occupies position 7 has been incremented by 5 to become m in the new string.

    the thing is I have absolutely no idea where to start!
    I've been working with the string class a bit on blueJ but i know only how to increment numbers with the ++ and still i can only do a number at a time. How can I do this with a whole string?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Take the letter and the index of it, get the int value of the character and do the increment. Then replace the latter with the current one, you've calculated.

    Did you try anything so far?

  3. #3
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    The thing is... it is not only 1 letter from a string... it has to be multiple letters that occupy specific (odd) positions. How can I increment multiple letters from different positions at the same time?

    Now i know indexOf is in the string class and I need somehow to equal that to an int....and then replace the string by a new one...but like I said, i have no idea how to get started on the actual code.

    Could you point me in the right direction? Ive been trying to write the pseudo code for a while but I got this blank page staring at me.

  4. #4
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    i got the idea so far

    Java Code:
    public class increment
    {
    
    public static void main (String args[]) {
    
    
          String str = "Welcome ";
          System.out.println(str);
    
          // charAt method returns the corresponding character to the passed index
          // in parentheses
          char letter = str.charAt(0), letter1 = str.charAt(1), letter2 = str
              .charAt(2), letter3 = str.charAt(3), letter4 = str.charAt(4), letter5 = str
              .charAt(5), letter6 = str.charAt(6), letter7 = str.charAt(7);
    
              letter2++;letter2++;letter2++;letter2++;letter2++;
          System.out.println(letter+""+letter1+""+letter2+""+letter3+""+letter4+""+letter5+""+letter6+""+letter7); 
    
    
    
    
    }
    }
    this make the 3rd letter change from 'l' to 'q'

    but the thing is...the string inputted could be longer than 7 characters...that would make my code useless..also is there a way to increment a letter 5 times
    other than doing letter1++;letter1++;letter1++;letter1++;letter1++; .... I need something like a loop dont I?

    please share your expertise with me everyone.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    > is there a way to increment a letter 5 times
    Not '5 times' but 'by 5'
    Java Code:
    letter1 += 5
    Be aware that if done indiscriminately, you can end up with characters that aren't letters.

    > I need something like a loop dont I?
    Yup, it sure looks like you do.
    Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db

  6. #6
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    okay with all your instructions which im really thankful for sharing with me... and after a bit of browsing the String class... ive managed to get this so far :


    Java Code:
          public class Main {
     
          /**
      
            * Converts a String to a character array
     
            */
     
          public void convertStringToCharArray() {
    
           
    
          String str ;
    
           
    
          System.out.println("Enter a string to increment");
      
          str = Clavier.lireString(); // this is a class made by my teacher which is similar to the scanner class to read inputted keyboard entrys by the user
    
    
          char[] cArray = str.toCharArray();
           
    
          for (char c : cArray)
     
          
          System.out.println(c+=5);
    
           
          }
    
          /**
    
            * Starts the program
    
            *
    
            * @param args the command line arguments
    
            */
    
          public static void main(String[] args) {
    
          new Main().convertStringToCharArray();
    
          }
     
          }
    this prints out every letter of the string incremented by 5 only vertically..

    like

    'abc' prints out :

    'f'
    'g'
    'h'

    i know it's my loop that is incorrect...how can i print out a new 'normal' string ?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    So use print(...) instead of println(...)

    db

  8. #8
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    okay so no ive got

    Java Code:
    String str ;
            
    System.out.println("Enter a string");
    str = Clavier.lireString(); //similar to scanner class
    
    
    char[] cArray = str.toCharArray();
    for (char c : cArray )
    System.out.print(c+=5);
    the input of abcdef gives me fghijk which is good only i wish to increment only pair positions in the string so i need something like a charAt(i)=i+2 in the for loop only i can't implement it in the for loop i have already. somehow.
    please help

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you say is not clear to me. Are you want to increment the number by 2 or 5? What you mean by "pair position"?

  10. #10
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    i want to increment it by 5...sorry for my english when i said pair position i meant odd position in the string.

    which means only charAt (1,3,5,7....etc...) gets incremented

    example : 'apple' = 'aupq'

    i need a for loop to do that, by the way my teacher doesn't want us to use arrays for this method.

    so far i wrote:

    Java Code:
    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.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Rather doing it on odd position, can do for the complete word in a loop? Did you get what I say? Just forget about the potion, do it for the complete word in a for loop.

  12. #12
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    i can't man , the assignement is to do only odd positions

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  14. #14
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    yes i know i need a for loop but i cant make one that does it what i want it to

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's because you didn't think about the basis, you try to start from the final solution. How to get it, and then stuck on that.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at the following code segment and try to understand the code. Let me know if you stuck on anything. I've added some comments there for your reference.

    Java Code:
    public class CharReplacer {
    
        private static String incrementerDeCinqImpaires(String chaine) {
    
            // Store the original string to later reference, think why?
            String str = chaine;
    
            // Get the each leter of the string in a loop, do you know this?
            for (int index = 0; index < str.length(); index++) {
                // Who to check that a numbere is odd or even?
                if (index % 2 != 0) {
                    // Same what you've done
                    char ch = str.charAt(index);
                    ch += 5;
    
                    str = str.replace(chaine.charAt(index), ch);
                }
            }
            return str;
        }
    
        public static void main(String args[]) {
            System.out.println(incrementerDeCinqImpaires("adam"));
        }
    }

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please think about the naming conversions as well, and about access modifiers. How to handle them.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  20. #20
    iluvjava is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Today 07:50 AM
    Eranga Oh no, why I've waste my time Darryl for this man.
    Today 01:30 AM
    Darryl.Burke Cross posts
    Incrementing every letter in a string that occupies an odd position. - Java Programming Forums
    Java Programming - Incrementing every letter in a string that occupies an odd position.

    db
    There's nothing wrong with posting in different forums...it's a free world ain't it?

    And Eranga i don't understand what you think that you did to help me yet so that you may have "lost" your time.

Page 1 of 2 12 LastLast

Similar Threads

  1. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  2. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:57 AM
  3. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM
  4. How to create auto-increment
    By Albert in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •