I want to make a simple loop that will only end when user chooses to quit. I am having problems with Scanner and I can't really pinpoint it but I think it might be the .close() function. I've tried to put .close() almost everywhere, but the error still persists. I also tried to remove the .close() but what happens is it keeps looping even though I chose Quit.
import java.util.Scanner;
public class CipherServiceTest {
static String keysquare;
public static void main(String args[]) {
System.setProperty("key.square","alpha"); //default key.square
keysquare = System.getProperty("key.square");
WhatToDo();
}
static void WhatToDo() {
int choice=0, datatype=0;
Scanner scan = new Scanner(System.in);
CipherServiceImpl cs = new CipherServiceImpl();
System.out.println("1. Encrypt a file");
System.out.println("2. Decrypt a file");
System.out.println("3. Quit");
System.out.print("What do you want to do(1/2/3): ");
choice = scan.nextInt();
while(choice != 3) {
System.out.println("1. Alpha");
System.out.println("2. Binary");
System.out.print("Choose data type(1/2): ");
datatype = scan.nextInt();
if (choice==1) {
if (datatype == 1) {
System.setProperty("key.square","alpha"); //default key.square
keysquare = System.getProperty("key.square");
cs.encrypt( );
}
else if (datatype ==2) {
System.setProperty("key.square","binary");
keysquare = System.getProperty("key.square");
cs.encrypt();
}
else {
System.out.println("Wrong Input");
//main(null);
}
}
else if (choice==2) {
if (datatype == 1) {
System.setProperty("key.square","alpha"); //default key.square
keysquare = System.getProperty("key.square");
cs.decrypt();
}
else if (datatype == 2) {
System.setProperty("key.square","binary");
keysquare = System.getProperty("key.square");
cs.decrypt();
}
else {
System.out.println("Wrong Input");
//main(null);
}
}
else{
System.out.println("Wrong Input");
//main(null);
}
//scan1.close();
main(null);
}
scan.close();
}
}
This is what happens in the console. When I choose quit the error appears. I tried choosing Quit as first option and it does work but the problem appears in the 2nd loop.
1. Encrypt a file
2. Decrypt a file
3. Quit
What do you want to do(1/2/3): 1
1. Alpha
2. Binary
Choose data type(1/2): 1
Open the message file
laih wdi
Open the key file
tae
1. Encrypt a file
2. Decrypt a file
3. Quit
What do you want to do(1/2/3): 3
1. Alpha
2. Binary
Choose data type(1/2): Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at CipherServiceTest.WhatToDo(CipherServiceTest.java:27)
at CipherServiceTest.main(CipherServiceTest.java:9)
CodePudding user response:
The root of the problem is main(null);
. Don't do that, your program starts all over again, you have managed to do something like hidden recursion and the flow gets all jumbled up. Just use the while loop. Step through the code with a debugger to better understand what's actually happening and why it's happening.
Also as a rule of thumb, don't close scanner, or anything that reads input from System.in
. This closes the underlying stream as well, and you don't want to close System.in
. This causes the exception you see, though it's not the root problem.
CodePudding user response:
Your second loop closes the scanner in your first loop,place use only one scanner