I'm taking the inputted message, adding 2 to the Unicode value of each letter, and then printing it out I'm having trouble including the spaces between the words. If the phrase was cheese pizza, it would print out "ejggj" and then stop at the space. How do I fix this?
package exercises;
import java.util.Scanner;
public class Ex10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Ask for message
System.out.println("Enter a string: ");
String message = scanner.next();
char[] array = message.toCharArray();
//Encode message
for(int i = 0; i < array.length; i++)
{
char letter;
letter = array[i];
if (array[i] =='y')
{
array[i] = 'a';
}
else if (array[i] == 'z')
{
array[i] = 'b';
}
else if (array[i] == letter)
{
letter += 2;
array[i] = letter;
}
else
{
letter = ' ';
}
}
//Give back encoded message
System.out.print("Encoded message: ");
System.out.println(array);
}//main
}//class