Results 1 to 20 of 25
Thread: increment letters from a string
- 03-17-2010, 02:53 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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?
- 03-17-2010, 03:10 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 03-17-2010, 03:23 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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.
- 03-17-2010, 03:52 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
i got the idea so far
this make the 3rd letter change from 'l' to 'q'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); } }
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.
- 03-17-2010, 05:23 AM #5
> is there a way to increment a letter 5 times
Not '5 times' but 'by 5'Be aware that if done indiscriminately, you can end up with characters that aren't letters.Java Code:letter1 += 5
> 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
- 03-19-2010, 04:36 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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 :
this prints out every letter of the string incremented by 5 only vertically..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(); } }
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 ?
- 03-19-2010, 07:24 AM #7
So use print(...) instead of println(...)
db
- 03-19-2010, 04:16 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
okay so no ive got
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.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);
please help
- 03-22-2010, 02:47 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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"?
- 03-22-2010, 02:54 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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:
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.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")); } }
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.
- 03-22-2010, 03:06 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-22-2010, 03:17 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
i can't man , the assignement is to do only odd positions
- 03-22-2010, 03:19 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-22-2010, 03:20 AM #14
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
yes i know i need a for loop but i cant make one that does it what i want it to
- 03-22-2010, 03:35 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-22-2010, 03:37 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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")); } }
- 03-22-2010, 03:37 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please think about the naming conversions as well, and about access modifiers. How to handle them.
- 03-22-2010, 06:30 AM #18
- 03-22-2010, 12:50 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oh no, why I've waste my time Darryl for this man.
- 03-22-2010, 03:11 PM #20
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
There's nothing wrong with posting in different forums...it's a free world ain't it?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
And Eranga i don't understand what you think that you did to help me yet so that you may have "lost" your time.
Similar Threads
-
Increment Operator Example
By abimaran in forum New To JavaReplies: 10Last Post: 11-03-2009, 04:45 PM -
Increment a Variable
By rhm54 in forum New To JavaReplies: 2Last Post: 06-14-2008, 02:57 AM -
validating a string for numbers and letters?
By lockmac in forum New To JavaReplies: 1Last Post: 08-09-2007, 09:17 AM -
How to create auto-increment
By Albert in forum JDBCReplies: 2Last Post: 07-04-2007, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks