0

The code asks if you want to encode or decode a message, then it asks for the message. It will work by this reference: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 "

"kngcadsxbvfhjtiumylzqropweKNGCADSXBVFHJTIUMYLZQROPWE,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 "

Therefore if you try to encode the letter 'a' for example, it will output the letter 'k'.

My problem is that I can't include any spaces when typing the message.

Here is my code:

import java.util.Scanner;

public class SecretMessage {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        do {
            System.out.println("Enter 1 to encode, 2 to decode, 3 to quit:");
            int start = input.nextInt();

            if (start == 3){
                break;
            }

            System.out.println("Type your message:");
            String test = input.next();
            String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 ";
            String enc = "kngcadsxbvfhjtiumylzqropweKNGCADSXBVFHJTIUMYLZQROPWE,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 ";

            char[] array = test.toCharArray();
            char[] decoded = letters.toCharArray();
            char[] encoded = enc.toCharArray();
            int[] position = new int[array.length];
            char[] end = new char[array.length];

            if (start == 1){
                for (int i = 0; i < test.length(); i++){
                    for (int j = 0; j < decoded.length; j++){
                        if (array[i] == decoded[j]){
                            position[i] = j;
                        }
                    }
                }

                for (int f = 0; f < test.length(); f++){
                    end[f] = encoded[position[f]];
                }

                for (int x = 0; x < test.length(); x++){
                    System.out.print(end[x]);
                }
                System.out.println(" ");
            } else {
                for (int i = 0; i < test.length(); i++){
                    for (int j = 0; j < encoded.length; j++){
                        if (array[i] == encoded[j]){
                            position[i] = j;
                        }
                    }
                }

                for (int f = 0; f < test.length(); f++){
                    end[f] = decoded[position[f]];
                }

                String output = new String(end);
                System.out.println(output);
            }
            System.out.println(" ");
        } while (1 ==1);

    }
}
  • 4
    Change `input.next()` to `input.nextLine()` to grab an entire line of input (`.next()` only scans a complete token) – GBlodgett Sep 28 '18 at 00:48
  • I tried to, but when I change it the code jumps immediately to the end after choosing between encode/decode/quit, and asks to choose again. – FELIPE DE ARAUJO Sep 28 '18 at 02:03
  • Try reading this: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – GBlodgett Sep 28 '18 at 17:41

0 Answers0