1

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?

Aledema
  • 11
  • 4
  • 1
    you may want to put an else after the if: `if(input.equals("j")){...}else{...}` otherwise you will call Integeger.parseInt on text. Also please print the stack trace in the catch with `e.printStackTrace()` and post it here. – Alexander Daum Aug 25 '18 at 08:26
  • Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Maurice Aug 25 '18 at 08:29
  • You try to call `Integer.parseInt()` on the string "m", which is not a number. I guess `Integer.parseInt()` is not the right method you want? What do you want to do with the user input if it is not j? – Alexander Daum Aug 25 '18 at 08:53
  • @AlexanderDaum What i want to do is create a new thread and instance of the class Parser if the input is "j", if the input is a number and that number is equal to one of the instances ids, i shut that instance. – Aledema Aug 25 '18 at 09:01
  • @Aledema I think I found the problem: you create the Scanner in a try with resource block, that means, at the end of the block the scanner will be closed, and so will be System.in. Then you get no more input. Create your scanner before the loop and use the same instance all the time, then it could work. – Alexander Daum Aug 25 '18 at 09:04
  • @AlexanderDaum Shouldn't the Scanner be recreated everytime the try/catch is called? – Aledema Aug 25 '18 at 09:10
  • @Aledema The scanner is recreated, but the System.in input stream is already closed, so the scanner wants to read from a closed input stream. – Alexander Daum Aug 25 '18 at 09:10
  • @AlexanderDaum Mhh I was of the idea that creating the Scanner would also re open the input stream, apparently there is a lot of stuff I don't know about Java yet. Thanks for the help, I'll try your suggestions and update the post – Aledema Aug 25 '18 at 09:18

0 Answers0