Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question: Scanner and nextLine

 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a file, named myfile.txt, which contains nothing in it except one linebreak (no text, and no more than one linebreak). When I run the following code, I don't get an exception:
import java.util.Scanner;
import static java.lang.System.out;
import java.io.*;
class More {
public static void main(String args[]) throws IOException {
Scanner scanner = Scanner.create(new File("myfile.txt"));

out.printf(">>%s<<\n", scanner.nextLine());
out.printf(">>%s<<\n", scanner.nextLine());
out.printf(">>%s<<\n", scanner.nextLine());
out.printf(">>%s<<\n", scanner.nextLine());
out.printf(">>%s<<\n", scanner.nextLine());
}
}
Instead, I get the following output:
>><<
>><<
>><<
>><<
>><<
So my question is, why is nextLine not giving me a NoSuchElementException? Shouldn't I be getting this exception when there are no more linebreaks to read?
Thanks.
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, looking at the 1.5Beta1 JavaDoc:

Throws:
NoSuchElementException - if no line was not found

So it will only throw an exception if there is a line to be found ;-)
Ignoring JavaDoc typos, this could conceivably be a bug in the library, since it looks like you're doing the right thing - I'd try and find it in the Bug Parade.
That said, maybe I'm reading it wrong, I have to admit I didn't try it myself.

--Tim
[ April 19, 2004: Message edited by: Tim West ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bug, apparently fixed for the beta 2 release (whenever that's available).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic