user3313947
user3313947

Reputation:

Java ignore spaces in user input

Basically, I'm making a caesar cipher program which encrypts or decrypts messages that the user enters. However, if the user enters spaces in the messages (e.g Stack Overflow), it will perform the encryption or decryption offset process on the spaces as well as the letters and obviously I don't want it to do that. I want it to ignore the spaces and print them in the encrypted or decrypted message.

Here's my code:

    Scanner myinput = new Scanner(System.in);   
    String plain;
    String encrypted; 
    char chars[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' '};

    System.out.println("Please enter your message."); 
    plain = myinput.nextLine().toUpperCase(); 
    myinput.close(); 

    char[] plaintext = plain.toCharArray(); 
    if(plain.isEmpty()) { 
            System.out.println("You need to give me a message to encrypt.");
    }

    for(int c = 0;c < plaintext.length;c++) { 
        for(int p = 0 ; p < 105;p++) { 
            if(p <= 100) { 
                if(plaintext[c] == chars[p]) {
                    plaintext[c] = chars[p + offset];
                        break;
                    }  
                }
            else if(plaintext[c] == chars[p]) {
                plaintext[c] = chars[p - 81];
            }
        }
    }
    encrypted = String.valueOf(plaintext);
    return encrypted; 

Upvotes: 0

Views: 2246

Answers (2)

You have some logical problem in your program, First problem of your program is that, there is no check of space ' ', thus the program don't know what to do when a space found, secondly you are adding palinttext[c]=chars[p+offset] so whatever it finds in text it adds it to palintext[p] including space ' '. So to solve this you need to add a check of space ' ' in your code and to avoid space you need to add another string ( output ) to store your required output, I am showing you the modified code:

    Scanner myinput = new Scanner(System.in);
    String plain;
    String encrypted;
    char chars[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'A', 'B', 'C', 'D', 'E',
            'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' };

    System.out.println("Please enter your message.");
    plain = myinput.nextLine().toUpperCase();
    myinput.close();

    char[] plaintext = plain.toCharArray();
    String output = ""; // adding a new string
    if (plain.isEmpty()) {
        System.out.println("You need to give me a message to encrypt.");
    }

    for (int c = 0; c < plaintext.length; c++) {
        for (int p = 0; p < 105; p++) {
            if (p <= 100) {
                if (plaintext[c] == ' ') {//checking for space
                    break;
                } else if (plaintext[c] == chars[p]) {
                    output += String.valueOf(chars[p]+offset);//storing value
                    break;
                }
            } else if (plaintext[c] == chars[p]) {
                if (plaintext[c] == ' ') {//checking for space
                    break;
                } else {
                    output += String.valueOf(chars[p]-81);//storing value
                }
            }
        }
    }
    encrypted = String.valueOf(output);
    return encrypted;

Upvotes: 0

Andrei Nikolaenko
Andrei Nikolaenko

Reputation: 1074

for(int p = 0 ; p < 105; p++) {
    if (plaintext[p] == ' ') continue;

that's all you need to keep spaces untouched, also remove them fom chars array

Upvotes: 1

Related Questions