1

I am creating a java program that uses a cipher to encode any message the user types in. It works flawlessly with single words like "hello" and "testing", but begins to fall apart when you add spaces, like the message "hello world." Here is the code:

import java.util.Scanner;
import java.util.ArrayList;
public class Code {

public static void main(String[] args) {
    Scanner shiftValue = new Scanner(System.in);
    System.out.print("Please enter shift value: ");
    int shift = shiftValue.nextInt();
    String alphabet = "abcdefghijklmnopqrstuvwxyz"; 
    Scanner input = new Scanner(System.in);
    String codeInput = "anything";
    int index = 0;

    while(!codeInput.equals("end")) {
        System.out.println();
        System.out.println("Please enter message: ");
        codeInput = input.next(); 

        for(int i = 0; i < codeInput.length(); i++){ 
            if(Character.isWhitespace(codeInput.charAt(i))){
                System.out.print(" ");
            }
            else {
                while(alphabet.charAt(index) != codeInput.charAt(i)){           
                        index++;                                    
                }

                if(index > 25 - shift){                     
                    index = index - (26 - shift);
                    System.out.print(alphabet.charAt(index));                       
                }
                else {
                    System.out.print(alphabet.charAt(index + shift));
                }           

            }
            index = 0;  
        }
    }

} //method

} //class

When I type start the program, it asks for a shift value, which decides how many letters to shift the cipher. After that, it goes into a while loop, forever asking the user for input, then outputting an encoded version of the input, until the word "end" is typed. In the eclipse console, it looks like this:

Please enter shift value: 3

Please enter message: 
hello
khoor
Please enter message: 
testing
whvwlqj
Please enter message: 

However, when I type multiple words with spaces between them, it looks like this:

Please enter shift value: 3

Please enter message: 
hello world
khoor
Please enter message: 
zruog
Please enter message: 

For some reason, instead of displaying both words in the same sentence format as the input, it encodes the first word, then goes through the entire while loop again before encoding the second word.

I have no idea why this happens, so I would appreciate any help or advice you guys could give me. Thank you for reading my post, and have a wonderful day.

1 Answer 1

1

The Scanner splits the input for you already and by default by whitespaces. JavaDoc:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.