-3

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 
1
  • if(letter==' ') continue; Jan 17, 2017 at 22:19

2 Answers 2

1

Mohsen_Fatemi is partly wrong. In the scope of this question he is completely wrong.

Your problem concerning your missing part of the sentence does not come from your loop and the conditions in it but from your use of the scanner.

The scanner.next() method will read until it encounter a white space, whether it be a space, a tab or a new line. You need to use nextLine() to get your full input.

Mohsen_Fatemi is right about your algorithm. But this question was not about it. Don't dismiss his input though.

3
  • Bruno Delor thank you! I understand what you meant and you helped a lot. My program works now.
    – user7432780
    Jan 17, 2017 at 22:49
  • You're welcome. Don't hesitate to read the official documentation of java as well as having a look the different methods the scanner class offers. I gave you a quick understanding of how Scanner work but the class can be told to use other things as a delimiter. By default it happens to be a white space but you can set it to other characters (to read a CSV for instance, even though it's not a efficient way to read a CSV file) Jan 17, 2017 at 22:53
  • @BrunoDelor mention my name again :) Jan 17, 2017 at 22:54
0

change your code int this way , you want to escape when you have space in your String , so i've been added a condition if(array[i]==' ') continue; when we see the space character we skip this round of loop and go to the next iteration

for(int i = 0; i < array.length; i++) {
       if (array[i] =='y'){
            array[i] = 'a';
       }else if (array[i] == 'z'){
             array[i] = 'b';
       }else{
             if(array[i]==' ') continue;
             array[i] += 2;
       }
}
2
  • @I. Adoubleo if you find it true , mark my answer as true , thank you Jan 17, 2017 at 22:26
  • You're not answering his question. You are right about his algorithm but his problem was somewhere else. Jan 17, 2017 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.