Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner error with nextInt() [duplicate]

I am trying to use Scanner to get an int from the keyboard, but I getting the following error:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at TableReader.mainMenu(TableReader.java:122)
    at TableReader.main(TableReader.java:76)

This is what I have. It is independent of the rest of my program, I don't understand why this isn't working. It is declared in a method that is being called in a while loop, if that helps.

    // scan for selection
    Scanner s = new Scanner(System.in);
    int choice = s.nextInt();           // error occurs at this line
    s.close();

I stepped through with the debugger and narrowed the error down to:

A fatal error has been detected by the Java Runtime Environment: SIGSEGV (0xb) at pc=0xb6bdc8a8, pid=5587, tid=1828186944

JRE version: 7.0_07-b30 Java VM: OpenJDK Server VM (23.2-b09 mixed mode linux-x86 ) Problematic frame: V [libjvm.so+0x4258a8] java_lang_String::utf8_length(oopDesc*)+0x58

Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

like image 409
spatara Avatar asked Oct 11 '12 04:10

spatara


People also ask

What does nextInt () do in the Scanner class?

nextInt() The nextInt() method of a Scanner object reads in a string of digits (characters) and converts them into an int type. The Scanner object reads the characters one by one until it has collected those that are used for one integer. Then it converts them into a 32-bit numeric value.

Why Scanner is not working in Java?

The reason for your problem is that following the preceding nextInt() , you're still on the same line, and nextLine() returns the rest of the current line. That is, nextLine() did not block for your input, because the current line still has an empty string remaining.

Can you reuse the same Scanner Java?

If you do not close the Scanner then Java will not garbage collect the Scanner object and you will have a memory leak in your program: void close(): closes the Scanner and allows Java to reclaim the Scanner's memory. You cannot re-use a Scanner so you should get rid of it as soon as you exhaust its input.

Why is nextLine not working?

It's because when you enter a number then press Enter , input. nextInt() consumes only the number, not the "end of line". When input. nextLine() executes, it consumes the "end of line" still in the buffer from the first input.


1 Answers

You should use the hasNextXXXX() methods from the Scanner class to make sure that there is an integer ready to be read.

The problem is you are called nextInt() which reads the next integer from the stream that the Scanner object points to, if there is no integer there to read (i.e. if the input is exhausted then you will see that NoSuchElementException)

From the JavaDocs, the nextInt() method will throw these exceptions under these conditions:

  • InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
  • NoSuchElementException - if input is exhausted
  • IllegalStateException - if this scanner is closed

You can fix this easily using the hasNextInt() method:

Scanner s = new Scanner(System.in);
int choice = 0;

if(s.hasNextInt()) 
{
   choice = s.nextInt();
}

s.close();
like image 98
Hunter McMillen Avatar answered Oct 20 '22 00:10

Hunter McMillen