Results 1 to 15 of 15
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default 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:

    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)));	
    		
    	}
    	
    }
    Just need to go from z to a using mod operator.
    Any help would be greatly appreciated!
    Last edited by hiei_yasha; 01-15-2011 at 02:23 AM. Reason: Moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Ok, I changed my "enter one letter" part to this:
    Java Code:
    System.out.println("Please enter one letter from a to z: ");
    		String alphabet = myScanner.next();
    		System.out.println(alphabet.charAt(0));
    This shows me my input.

    Tried this code but it does the same thing.
    Java Code:
    System.out.println(name.replace(alphabet, (alphabet+1)));
    Should alphabet be charAt(0) and then charAt(0)+1?

    Sorry fubarable, your explanation got me thinking of what to do, I just don't know HOW to do that.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Change the string to char:

    Java Code:
    char alphabet = myScanner.next().charAt(0);
    System.out.println(name.replace(alphabet, (alphabet+1)));
    --user0--

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  7. #7
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by user0 View Post
    Change the string to char:

    Java Code:
    char alphabet = myScanner.next().charAt(0);
    System.out.println(name.replace(alphabet, (alphabet+1)));
    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));

  8. #8
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    tried this:
    Java Code:
    char alpha = alphabet.charAt(0);
    System.out.println(name.replace(alpha, alpha+1));
    This results in a compile error as well (char,int) ?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    String let = scan.nextLine();
    char nextLet = let.charAt(0);
    nextLet += 1;
    now let will be the original String, and String.valueOf(nextLet) will be the next letter string
    Last edited by sunde887; 01-15-2011 at 12:09 AM.

  10. #10
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    why are you printing charAt(0)? Why not store it, then manipulate it, then convert it to a string and use replace with the original input and the new variable.
    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'));

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  13. #13
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You seem to be ignoring my recommendations. So I'm out of here. Best of luck.
    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:
    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, (char)((int)alpha+1)));
    I'll try your suggestions for the z->a thing

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    }

  15. #15
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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
    }
    our prof told us to try to figure it out using the mod operator, unfortunately he hasn't even gone over how it ties in with letters and all that. i get that each letter has a number value associated with it, but :/

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  2. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  3. Replacing Else If
    By jingly99 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2009, 11:06 PM
  4. Replacing char in string help
    By jimmy-lin in forum New To Java
    Replies: 3
    Last Post: 10-12-2009, 06:01 AM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM

Posting Permissions

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