Whenever I run the java code below it compiles but the line that include the replace method seems to be skipped, such so that the inputted string and the output (newMessage) are the same. Why? variable C and variable D are chars...
import java.util.Scanner;
public class javaencrypt
{
public static void main(String[] args)
{
// define and instantiate Scanner object
Scanner input = new Scanner(System.in);
//prompt user to enter a string
System.out.println("Please enter a string: ");
String message = input.nextLine();
String newMessage = message;
char c=' '; // the character at even locations
char d=' '; // new character
// go throughout the entire string, and replace characters at even positions by the character shifted by 5.
// access the even characters with a for loop starting at 0, step 2, and ending at length()-1
// for( initial value; maximum value; step)
for(int k=0; k<message.length(); k=k+2)
{
c=message.charAt(k);
d=(char)(c+5);
/*
there will always be characters available, because keyboard is mapped on ASCII which is in the beginning of UNICODE
*/
newMessage.replace(c,d);
}
System.out.println("Message replacement is: " + newMessage);
}
}