I'm fairly new to Java and programming in general, I'm working on a program which is supposed to get input from user and based on that create/delete class instances.
Here is the code:
while(true) {
int is = -1; //0 is the number of first istance
try(Scanner s = new Scanner(System.in);){
System.out.print("Enter the desired action: ");
String input = s.next();
if(input.equals("j")){
(new Thread(new Parser())).start();
}
is = Integer.parseInt(input);
if (is >= 0 && is <= (Parser.count - 1)) {
Parser.kill(is);
}else {
System.out.println("wrong key");
break;
}
}catch(NumberFormatException e){
System.out.println("exception");//
}
}
The main issue is that it throws "NoSuchElementException" if the input is not "j", I tried to catch the exception, but in that case the program goes in a loop and stop waiting for user input. Here is the stack trace:
java.lang.NumberFormatException: For input string: "m"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Task.run(Task.java:17)
at java.lang.Thread.run(Unknown Source)
Could you point me out to what I'm doing wrong? Is there any library that could help me making a cmd like program?