-2

I am trying to create a cipher game. Our teacher said there should be 2 modes. First mode should be a normal mode where the quote will be choosen randomly. Second mode is the test mode which lets you choose a quote. In the test mode I can't go further because it says terminated, I don't know what the problem is.

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Random random = new Random();


         char plainText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
         char cipherText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};

        System.out.println("Please choose a mod: ");
        System.out.println("1.Normal Mode");
        System.out.println("2.Test Mode");
        int user = in.nextInt();

        String[] strings = {
                "So  many  books  so  little  time ",
                "Be  the  change that  you  wish to  see  in  the  world ",
                "No  one  can  make  you  feel  inferior  without  your  consent",
                "Love  for  all  hatred  for  none ",
                "Die  with  memories  not  dreams",
                "Aspire  to  inspire  before  we  expire",
                "Whatever  you  do  do  it  well",
                "What we  think  we  become ",
                "Be so good they cant ignore you ",
        };

        String randomString = strings[random.nextInt(strings.length)];     


        if (user==1) {
            System.out.println(randomString);           
            for (int a=0;a<randomString.length();a++) {
                for (int i=0; i<plainText.length;i++) {
                    if(plainText[i] == (randomString.charAt(a))) {
                        System.out.print(cipherText[i]);
                    }
                }
            }
        }
        else if(user==2) {
            System.out.println("Please choose one quote: ");
            String islemler = "1. So  many  books  so  little  time\n" +
            "2. Be  the  change that  you  wish to  see  in  the  world\n" +
            "3. No  one  can  make  you  feel  inferior  without  your  consent\n" +
            "4. Love  for  all  hatred  for  none\n" +
            "5. Die  with  memories  not  dreams\n" +
            "6. Aspire  to  inspire  before  we  expire\n" + 
            "7. Whatever  you  do  do  it  well\n" +
            "8. What we  think  we  become\n" +
            "9. Be so good they cant ignore you\n";

            System.out.println(islemler);
            String islem = in.nextLine();

            switch(islem) {
            case "1":
                System.out.println("So many books so little time");
            case "2":
                System.out.println("Be  the  change that  you  wish to  see  in  the  world");
            case "3":
                System.out.println(" No  one  can  make  you  feel  inferior  without  your  consent");
            case "4":
                System.out.println(" Love  for  all  hatred  for  none");
            case "5":
                System.out.println("Die  with  memories  not  dreams");
            case "6":
                System.out.println("Aspire  to  inspire  before  we  expire");
            case "7":
                System.out.println("Whatever  you  do  do  it  well");
            case "8":
                System.out.println("What we  think  we  become");
            case "9":
                System.out.println(" Be so good they cant ignore you");
            }

        }
        else {
            System.out.println("Please restart the game");
        }
    }

}
Marcel Kämper
  • 295
  • 4
  • 13
  • 1
    Who sais "terminated"? Can you please clraifiy what exactly you mean? Does the program end unexpectedely? Did you debug the issue and looked where your progra exited? – HimBromBeere Feb 10 '20 at 14:48
  • when i choose the test mode its become terminated. I cant choose a quote – Never Crazy Feb 10 '20 at 14:50
  • So you only get "Please restart the game"? Once again: you should use your debugger to step line by line in order to see which variable has which value. I bet `user` isn´t what you expect it to be. – HimBromBeere Feb 10 '20 at 14:52
  • No what i mean is when i choose the test mode i need to choose one of the quote but i cant because it says already terminated – Never Crazy Feb 10 '20 at 15:01

3 Answers3

1

You need to re-initialize the Scanner in element

System.out.println(islemler);
in = new Scanner(System.in);
String islem = in.nextLine();

and also add break; for each switch-case

0

You forget add break; for each switch-case in test mode.

Use in.next() instead of in.nextLine()

According to this site, nextLine() will return

the line that was skipped

If I guest right, it will return null to islem. Also since you do not set default in switch-case, so the program bypassed switch-case and terminated.

Community
  • 1
  • 1
rk0_c
  • 1
  • 3
0

The second in.nextLine() is skipped because:

That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.

See: Scanner is skipping nextLine() after using next() or nextFoo()?

From attached stack issue the solution is: Workaround:

  • Either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.nextFoo to consume rest of that line including newline
int option = input.nextInt();
input.nextLine();  // Consume newline left-over
String str1 = input.nextLine();
  • Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. For example, you may convert to an integer using Integer.parseInt(String) method.
int option = 0;
try {
    option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
String str1 = input.nextLine();
skipper3k
  • 171
  • 1
  • 1
  • 8