I was trying to help this user with an answer, but reading in the file isn't working as I expected.
If I create a file with just the OPs first two lines:
Learning Java - William Lane -12.00 - Wiley - 0811234561
- William Stalling - 25.00 - Prentice Hall - 1304086641
The output is as expected:
Learning Java - William Lane -12.00 - Wiley - 0811234561
- William Stalling - 25.00 - Prentice Hall - 1304086641
But when I add the third line:
Learning Java - William Lane -12.00 - Wiley - 0811234561
- William Stalling - 25.00 - Prentice Hall - 1304086641
OOP programming - Graham Winter - 32.50 – O'Reilly - 0471974555
The output is blank.
My code is straight forward, just to test it. I copied and pasted the file contents directly from the linked question above. Is there a special character on that line, and if so, how to find it? And why would it not print the other lines?
Scanner sc = new Scanner(new File("myfile"));
while (sc.hasNextLine()) {
String nameLine = sc.nextLine();
System.out.println(nameLine);
}
I ran it through a debugger and sc.hasNextLine
returns false
and the program ends.
Edit:
I'm using Notepad
basically to test this, and noticed (on the browser) that the dash between 32.50 and O'Reilly is slightly elongated. So I removed this dash and saved and the program works as expected. When I put it back in and save and re-run the program, it doesn't display anything. I can produce this every time.
I then tested in Notepad++
and all worked smoothly. Happy days.
So I did a comparison between the two files with WinMerge
; it basically says the only difference is that dash, and on Notepad++
, it's –
but on Notepad
, it's –
.
Anyone any ideas?
You may try these changes to see if it works for you. In your post i copied your 3 lines put it to notepad and saved and then opened in notepad++. It never gave issue.
THis might probably because of difference in file encoding/character set. Try reading the file as UTF-8 mode and check.