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