Results 1 to 15 of 15
Thread: Replacing a char with the next
- 01-14-2011, 10:39 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Replacing a char with the next
Hi I'm very new to java so please excuse any stupid questions on my part!
I tried searching the forum, java API, and the net in general for how to replace a specified character with the one following it in the alphabet. I understand replace('this', 'that) but finding difficulty in replacing it with the following letter. This is an assignment for my java class.
In the assignment we are to :
prompt for a name,
display the length of the name,
ask for a number less than and greater than length of name,
display the first and last letter of the name,
do a few substring things (not important),
and then prompt the user for a letter, which will then be replaced by the letter after it. For example if my name is "sassy" and input "s", I want it to display "tatty". Also if my name is "zebra", and input "z", I want it to display "aebra", incorporating the mod operator.
This is what I have so far:
Just need to go from z to a using mod operator.Java Code:import java.util.Scanner; public class Replaceme { public static void main (String [] args) { Scanner myScanner = new Scanner(System.in); //Asking the user for their name System.out.println("What is your name? "); String name = myScanner.nextLine(); System.out.println("Your name is: " + name); //Printing the length of name before prompting for a number int length = name.length(); System.out.println("The length of your name is: " + length); //Asking the user for a number smaller than length of name System.out.println("Please enter a number less than the length of your name."); int lesslength = myScanner.nextInt(); System.out.println(lesslength); //Asking the user for a word longer than name System.out.println("Please enter a word that has more characters than your name."); String longername = myScanner.next(); System.out.println(longername); //First letter of name System.out.println("The first letter of your name is: "); char char0 = name.charAt(0); System.out.println(char0); //Last letter of name System.out.println("The last letter of your name is: "); char charlast = name.charAt(name.length() -1); System.out.println(charlast); //Substring 0 to number entered of second string System.out.println(longername.substring(0,lesslength)); //Substring number entered to end of second string System.out.println(longername.substring(lesslength,longername.length())); //Prompting user for single letter System.out.println("Please enter one letter from a to z: "); String alphabet = myScanner.next(); //Results of original name concatenated with second string without operator System.out.println(name.concat(longername)); //Print value of integer user entered System.out.println(lesslength); //Replacing single letter entered with corresponding letter in alphabet char alpha = alphabet.charAt(0); System.out.println(name.replace(alpha, (char)((int)alpha+1))); } }
Any help would be greatly appreciated!Last edited by hiei_yasha; 01-15-2011 at 02:23 AM. Reason: Moderator edit: code tags added
-
To increment a letter to the next, you would need to get the char, add one to it, and then turn it into a String. To elaborate, you can change a letter in a String to a char by using the String charAt(int i) method. If your String has only one letter, as yours will, then you will call charAt(0) to convert the single letter at the 0 position into a char. Then if this isn't 'z', you'll increment the char by 1, and if it is 'z', you'll change the char to 'a'. Then you can convert a char back into a String using the String.valueOf(char c) method.
Last edited by Fubarable; 01-14-2011 at 10:55 PM.
- 01-14-2011, 11:09 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Ok, I changed my "enter one letter" part to this:
This shows me my input.Java Code:System.out.println("Please enter one letter from a to z: "); String alphabet = myScanner.next(); System.out.println(alphabet.charAt(0));
Tried this code but it does the same thing.
Should alphabet be charAt(0) and then charAt(0)+1?Java Code:System.out.println(name.replace(alphabet, (alphabet+1)));
Sorry fubarable, your explanation got me thinking of what to do, I just don't know HOW to do that.
-
I would break it down into separate steps:
1) get single letter input String
2) Convert to char using charAt(0)
3) Check if == 'z' and if so, change char to 'a'. If not add 1 to char. (note this can also be done by using the mod operator, but you would first need to shift all the letters down by 'a' and then shift them back up again, so I recommend just using a simple if/else block)
4) Create String nextLetter and create using char above.
5) Call replaceAll on the name String using the original single letter String and the nextLetter String.
YMMV.Last edited by Fubarable; 01-14-2011 at 11:18 PM.
- 01-14-2011, 11:13 PM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Change the string to char:
Java Code:char alphabet = myScanner.next().charAt(0); System.out.println(name.replace(alphabet, (alphabet+1)));
--user0--
- 01-14-2011, 11:16 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A character is represented as a number, so just adding one to that number changes the character, your goal is to switch the input to a char, which can be thought of as a number, add 1 to that number then when you switch it back it produces the next letter.
if they input "a", when you switch it to a char it is also the number 97, so adding one gets you the next char, b, or 98. Take the char, convert it back to a String, then search and replace the name.
- 01-14-2011, 11:26 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
When I try this it says that alphabet is already defined in main. So far:
Java Code:System.out.println("Please enter one letter from a to z: "); String alphabet = myScanner.next(); System.out.println(alphabet.charAt(0)); char alphabet = myScanner.next().charAt(0); System.out.println(name.replace(alphabet, alphabet+1));
- 01-14-2011, 11:55 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
tried this:
This results in a compile error as well (char,int) ?Java Code:char alpha = alphabet.charAt(0); System.out.println(name.replace(alpha, alpha+1));
- 01-15-2011, 12:00 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
why are you printing charAt(0)? store the input in some string variable, then switch it to a char and store the value + 1 in a new variable, finally, change the new variable to a string
now let will be the original String, and String.valueOf(nextLet) will be the next letter stringJava Code:String let = scan.nextLine(); char nextLet = let.charAt(0); nextLet += 1;
Last edited by sunde887; 01-15-2011 at 12:09 AM.
- 01-15-2011, 12:07 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Does printing it make it so it can't be stored? You have to realize this is my 2nd week in java class, so again I apologize if things that may seem simple to everyone may not be to me.
Alright, so I tried this and got my char to be replaced with 'x'. Still, when i do alpha+1, it has problems.
Java Code:System.out.println("Please enter one letter from a to z: "); String alphabet = myScanner.next(); char alpha = alphabet.charAt(0); System.out.println(name.replace(alpha, 'x'));
- 01-15-2011, 12:10 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im not trying to be mean just asking for your thought on why, re read my last post, I edited it to help with what you want to do.
Also, as a side note, I kind of did almost the same thing fubarable suggeste.
-
You seem to be ignoring my recommendations. So I'm out of here. Best of luck.
- 01-15-2011, 12:59 AM #13
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Believe me I read what you wrote throughout the thread, unfortunately it was still too confusing for me. It's like telling me how to make a chair by saying "take the legs and screw them onto the seat." I needed to know how to do that, I guess with more intermediate steps, or in this case examples of what the code would look like.
I finally got what I wanted with:
I'll try your suggestions for the z->a thingJava Code:System.out.println("Please enter one letter from a to z: "); String alphabet = myScanner.next(); char alpha = alphabet.charAt(0); System.out.println(name.replace(alpha, (char)((int)alpha+1)));
- 01-15-2011, 01:41 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
if you dont do the z ->a thing, if the user types z it will switch everything with {, it's quite a simple change, make an if else clause,
Java Code:if(input.equals("z")) { replace all occurences of z with a } else { normal procedure }
- 01-15-2011, 02:12 AM #15
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
check a string char by char
By Sotsiak in forum New To JavaReplies: 2Last Post: 10-23-2010, 09:24 PM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 05:48 AM -
Replacing Else If
By jingly99 in forum Advanced JavaReplies: 5Last Post: 12-28-2009, 11:06 PM -
Replacing char in string help
By jimmy-lin in forum New To JavaReplies: 3Last Post: 10-12-2009, 06:01 AM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks