简体   繁体   中英

Using BufferedReader or Scanner for processing multiple inputs in Java

When I am trying to Read all the lines by using this program...It gives me one line less as an output.

Sample Program:

String line = null;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) 
{
  line = scanner.nextLine();
  System.out.println(line);
}
scanner.close();

When I give the Standarad input as:

ab
cd
ef
gh

Output is:

ab
cd
ef

"scanner.hasNextLine()". The last line doesn't have a nextline, so it exists the loop before it prints out ef.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM