0

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
Amit
  • 45,440
  • 9
  • 78
  • 110

1 Answers1

-1

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

A.sharif
  • 1,977
  • 1
  • 16
  • 27
  • Why do you think it exits the `while` loop? – Sotirios Delimanolis Jun 29 '15 at 18:50
  • @SotiriosDelimanolis I think the code works actually... tested it myself. – jstnchng Jun 29 '15 at 18:52
  • @jstnchng It will "work" (as in, it will print the last line) if you add an extra carriage return at the end of the input. OP doesn't seem to be doing that. – Sotirios Delimanolis Jun 29 '15 at 18:53
  • He is giving the input via a file. When the loop is on ef. "Line = scanner.nextLine()" will push the index to line "gh". Then on the next loop it checks the next line. line "gh" has no "nextLine" so it will exit the loop before printing anything on line "gh". My answer is right, you shouldn't have down voted it :/ – A.sharif Jun 29 '15 at 18:57
  • Why do you think it's a file when they've specifically shown `System.in`? – Sotirios Delimanolis Jun 29 '15 at 18:58
  • And when they write _When I give the Standarad input as [...]_. – Sotirios Delimanolis Jun 29 '15 at 18:58
  • And, regardless, `hasNextLine` will stop when it sees the end of stream, returning the buffer before the end of stream. – Sotirios Delimanolis Jun 29 '15 at 18:59
  • You can pass in a file through console when you run the program. You don't need to specify the file name in the code. The reason why I think it's a file is because of how he has word it and that it doesn't make sense otherwise. – A.sharif Jun 29 '15 at 19:04
  • I am not using the file as input.I am giving it as a System input. – Bishnu Trivedi Jul 01 '15 at 03:37
  • I have used concept of BufferedReader too..But I am facing the same problem.It would be helpful if you guys post the code snippet too :) – Bishnu Trivedi Jul 01 '15 at 03:38
  • @Bishnu if you're entering the input one at a time, then there should be no delay. I even tried it. You can try it here as well: http://www.browxy.com/SubmittedCode/298675 – A.sharif Jul 01 '15 at 04:06
  • @Sharif..what I was doing is I copied the string and gave it in System Input.I gave the input in one shot. This is where I faced the problem as I was not able to get the last string. – Bishnu Trivedi Jul 01 '15 at 08:40
  • @Bishnu, So that's kinda how I first thought you were doing it. In one big group (like in a file). I can't even duplicate it by copy and pasting your input into the console. Can you try putting your input into a file and feeding it in? Also does the scanner close after you paste the string as input? Or does it keep on going waiting for another input? – A.sharif Jul 01 '15 at 14:17