Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Scanner hasNextLine() problems

835847Feb 24 2011 — edited Feb 25 2011
I am using:
Scanner input = new Scanner(System.in)

For (int i + 0; i < 10; i++){

     while(input.hasNextLine()){
          String data = input.nextLine();
     }
}
Repeated calls to hasNextLine() beyond the first cause the while loop to fail.

I tried the following:
Scanner input;

For (int i = 0; i < 10, i ++){

     input = new Scanner(System.in);

     while(input.hasNextLine()){
          String data = input.nextLine();
     }
     input.close();
}
It was, as you can guess, not successful. Essentially, once Scanner has moved to the end of the buffer and is at EOF, and further hasNextLine() produces false.
however, hasNextLine() is supposed to block for input so why it does not and allow further input is beyond my understanding.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2011
Added on Feb 24 2011
18 comments
2,112 views