0

I am having trouble with this assignment. The prompt is to have the user enter a message to be encoded. The encoding will take the messages and shift each character 4 spaces to the right according to the ASCII table. I think I have the encrypt and decrypt methods correct (?) but I can't tell because I cannot figure out how to get the string that's entered into the methods.

import java.util.Scanner;

public class EncryptDecrypt {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to my encrypting/decrypting program");
        System.out.println("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ");
        System.out.println("(1) Encrypt a message.");
        System.out.println("(2) Decrypt an encrypted message.");
        System.out.println("(3) Exit.");
        System.out.print("Your choice? ");
        int choice = input.nextInt();
        if (choice != 3) {
            if (choice == 1) {
                System.out.println("Please enter a message to be encrypted: ");
                String message = input.nextLine();
                System.out
                        .println("The encrypted string is " + encrypt(message));

            }
            else {
                System.out.println("Please enter a string to be decrypted: ");
                String encrypted = input.nextLine();
                System.out.println(
                        "The decrypted string is " + decrypt(encrypted));
            }

        } else {
            System.out.println("The program has been exited.");
        }
    }

    public static String encrypt(String message) {
        String encrypted = " ";
        for (int i = 0; i < message.length(); i++) {
            encrypted += (char) (message.charAt(i) + 4);
        }
        return encrypted;

    }

    public static String decrypt(String encrypted) {
        String unencrypted = " ";
        for (int i = 0; i < encrypted.length(); i++) {
            unencrypted += (char) (encrypted.charAt(i) - 4);

        }
        return unencrypted;
    }
}
T. Rowen
  • 1
  • 1
  • what do you see as message after the user enters a string? – AbtPst Nov 02 '15 at 21:16
  • It never gets to that point. when I run the program and enter a choice (1, 2, or 3) it goes to that choice and prints out this..."Welcome to my encrypting/decrypting program _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (1) Encrypt a message. (2) Decrypt an encrypted message. (3) Exit. Your choice? 1 Please enter a message to be encrypted: The encrypted string is BUILD SUCCESSFUL (total time: 1 second) – T. Rowen Nov 02 '15 at 21:58

2 Answers2

0

Hey i found out that it has to do something with the Line.

int choice = input.nextInt();

I don't know why that doesn't work, but i suggest that you could use

int choice = Integer.parseInt(input.nextInt());

instead. That does work, but sadly I have no idea why.

CodeX
  • 1
  • 4
  • Thanks for the response but I think it has something to do with my if statement. When I run the program and input a choice of 1, 2, or 3. It goes to that selection and just runs the statements and ends the program without letting the user input a string – T. Rowen Nov 02 '15 at 22:02
  • No it isn't the if Statement. If you set choice directly to 3 without the input from nextInt, it will run correctly, so I'm sure it has something to do with the nextInt method. – CodeX Nov 02 '15 at 23:03
0

So my brother helped me and figured out that I needed to open up a new Scanner in choices 1 & 2 and then it let the user input a string and went through the rest of the code.

T. Rowen
  • 1
  • 1
  • 1
    So you didn't bother to read the duplicate question (see the first comment of your question)? – Tom Nov 02 '15 at 23:06