This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner not working  RSS feed

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never liked the Scanner object for getting input, but there seems to be a lot of users here that use it, so I wanted to become more familiar with it. I looked in the API and put together the code that I thought would do the trick to read a text file, but it didn't work. So I looked on the web and to my surprise, then chain of commands were identical to what I have. So I decided to do a quick BufferedReader from a FileReader from the same File I was using for input on the Scanner object, and to my surprise and chagrin it worked.

So my question is: Are my eyes just missing something in the Scanner portion of the code? No exceptions are thrown, and no errors are produced, it just never loops from the while clause in the Scanner object portion of the code.

Here is the Exception that is thrown if I try to read using the Scanner object and not check:

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at JHAMPracticeTest.getPool(JHAMPracticeTest.java:16)
at JHAMPracticeTest.main(JHAMPracticeTest.java:28)

But the BufferedReader works with the readLine().
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried your exact code, and it read each line from a file I created to test.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my machine it works just fine. Must be something with the file path you passed as f.
And instead of File, you should try to stick to Path. Check here.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn wrote:I just tried your exact code . . .
Are you sure about that? I think the loop opens on line 8 but never closes because there is a } missing. Edit: I have found the missing } and I am sorry for my mistake about that bit.

Assuming you inserted he missing } without noticing: does the code run normally if repeated? Does it run after using a buffered reader? I notice there is no sign of any code to close the reader. Is there any chance that the file has not been closed correctly before using the Scanner?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:I have never liked the Scanner object for getting input,

Finally! Somebody else who agrees with me.

But the BufferedReader works with the readLine().

Simple answer (from what you've supplied): Dunno.

It ought to work unless, as Campbell suggested, this isn't the only place you use 'f', and you didn't close() it first.

Winston
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. The code I supplied is just about all of the app, there is the part that declares the Class and the method and the call from the main method (none of changes with the change to the BufferedReader), but pretty much all that is there is a read loop for the scanner and it was converted--well as much as possible--to the BufferedReader. All the web examples I can find are exactly as I have in my little bit of "play with it code", so I have to go with what Winston has said: "Dunno." And just mark it down as something on my box between the hardware and Windows 10.

I will consider this as my question has been answered.

Winston Gutkowski wrote:
Les Morgan wrote:I have never liked the Scanner object for getting input,

Finally! Somebody else who agrees with me.

But the BufferedReader works with the readLine().

Simple answer (from what you've supplied): Dunno.

It ought to work unless, as Campbell suggested, this isn't the only place you use 'f', and you didn't close() it first.

Winston
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can run this all day long, again and again and again from the buffered reader. All code except the actual Scanner and BufferedReader segments are in common. There is no other code except normal overhead to declare the class and methods, but all of that is held in common with the BufferedReader--so the only thing that changes is in the code I posted and it changes exactly as shown and described.

I have to go with Winston on this and just scratch my head and put it down to the laptop and Windows 10. If I get curious enough today, maybe I will migrate it to another box and see if it works there--which it probably will from the results others have posted.

Thank you one and all. I will just put this down as answered, and it doesn't work on my box--at least the one I was trying it on--I had to give it 4 attempts to upgrade to Windows 10, so nothing really surprises me about that box except that it runs anything.

Campbell Ritchie wrote:
Keith Lynn wrote:I just tried your exact code . . .
Are you sure about that? I think the loop opens on line 8 but never closes because there is a } missing. Edit: I have found the missing } and I am sorry for my mistake about that bit.

Assuming you inserted he missing } without noticing: does the code run normally if repeated? Does it run after using a buffered reader? I notice there is no sign of any code to close the reader. Is there any chance that the file has not been closed correctly before using the Scanner?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything common about the files you've tested this with?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,
The file is the ARRL Extra Class Question Pool for the current cycle. It is less than 300KB, very small, and I have edited it with notepad to strip any leading white space from the file, so there is at least one block of good text in the file.
Les
PS: the thing that is baffling is that is will read with the BufferedReader, but not the Scanner object, and the Scanner object gives no errors nor any Exceptions.
PPS: as I said, I have to chalk this up to my laptop and it's copy of Windows 10.
Dave Tolls wrote:Is there anything common about the files you've tested this with?
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:and it doesn't work on my box--at least the one I was trying it on
I experienced that too. Few years back, with the couple lines of code. It worked perfectly fine on my work machine, but didn't on my personal. The 3rd line on OS X didn't behave as expected, window didn't pop up, while on Windows it worked fine.
After removing that second line, window popped up on OS X. So I thought something is wrong with showOptions method, which was very simple.

Then finally Campbell Ritchie told me I was using wrong technique for JFileChooser, I've tried to research it after, tried quite few different approaches, but didn't get nowhere. I was thinking if Java stopped being a platform independent (that is of course unlikely). So left it out with a blame to OS X.

Maybe similar anomalies happening to you too.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so here is an update to my quandary:
The file that I originally tried does not work under any conditions--I moved to testing on a different box; no go, I tried editing it; no go, I tried copying a paragraph or the content to another file; no go, I tried taking all characters not specifically a-z and A-Z out of the new file with the old chapter; no go.

So I tried a different smaller file--no go.

I tried creating a file in notepad--it works, as defined by my program.

I tried another file on my box--no go.

I tried another file on my box--and it works too.

So, what could be the commonality? My Theory is: The file I created using notepad was a native text file, never converted from another format, the one from the ARRL was converted to text from a Word Doc. The other files, instructions from various programs and hardware that I have, that did not work--well, I have to assume that they were converted from another format too. The one readme.txt that worked, I have to assume it was not converted from another file format--or if it was it was done with a more thorough/better program than all the others were.

So live and learn--I really don't like the Scanner class!!!
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les,

Wait

Exchange line 3 with
OR
Line 7 whats within parentheses exchange with
And please tell outcome.

I've tried to read *.doc file with File passed to Scanner - didn't read, while BufferedReader along with FileReader did.
Since Java 7 File considered as legacy, link.

Recommended to use is Path. So I did, and *.doc file was read by Scanner in the same way as with BufferedReader.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that works... so what I have to say is that I still really do not like Scanner!

Liutauras Vilda wrote:Les,

Wait

Exchange line 3 with
And please tell your outcome.

I've tried to read *.doc file with File passed to Scanner - didn't read, while BufferedReader along with FileReader did.
Since Java 7 File considered as legacy, link.

Recommended to use is Path. So I did, and *.doc file was read by Scanner in the same way as with BufferedReader.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a cow. An interesting topic, I learned something new.
Never experienced before that File can cause a serious issues used along with Scanner class.

And I think this time you got less exclamation marks
Les 2 posts ago wrote:I really don't like the Scanner class!!!

Most recently Les wrote:I still really do not like Scanner!

That might will even more change one day
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree that this is interesting. The articles that I have been reading are back from Java 7, so I have to assume that prior to Java 8, what I was doing not only worked, but was the norm. It seems that it has been broken in Java 8 though.

Liutauras Vilda wrote:Have a cow. An interesting topic, I learned something new.
Never experienced before that File can cause a serious issues used along with Scanner class.

And I think this time you got less exclamation marks
Les 2 posts ago wrote:I really don't like the Scanner class!!!

Most recently Les wrote:I still really do not like Scanner!

That might will even more change one day
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raise a bug!


(and I have never been a fan of Scanner either)
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Raise a bug!


(and I have never been a fan of Scanner either)
But there is nothing wrong with Scanner. Just need to use it along with right tools instead of legacy ones.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:I have to agree that this is interesting. The articles that I have been reading are back from Java 7, so I have to assume that prior to Java 8, what I was doing not only worked, but was the norm. It seems that it has been broken in Java 8 though.

Hmmm. Seems a bit premature, particularly as you're using what you freely admit is a somewhat shaky version of Windows 10.

Is it possible that the OS has introduced something that makes Java behave different? (I wouldn't put it past MS to do that deliberately)

I think you'd have to try your original file (the one that was causing problems), with a few combinations of Java and OS version before you can say definitively where the problem lies.

Good luck though. And fun topic ... although maybe you don't think so.

Winston
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beg to disagree.
The number of threads that have turned up around Scanner and the issues with when a token is kept or not in the buffer. Well, I don't have enough fingers and toes.


It's a class that seems simple, but has enough niggles in it that it traps the unwary. And any class that does that is problematic in my opinion.

(Oops, that was in reply to Liutauras, in case it causes confusion)
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They make the mistake of describing Scanner as a simple text Scanner. There ain't anything simple about it. Then they give correct documentation about nextLine, but it isn't clear, so most of the books copy it wrong. I think the simple thing means that it scans simple text as opposed to rich text format.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would beg to differ with your evaluation there. Here is a quote from the API:

Scanner(File source)
Constructs a new Scanner that produces values scanned from the specified file.

Now that does not work, in many cases, but it is from the Java 8 API Docs, so Scanner is indeed broken. I would also have to disagree with your definition of "legacy". If it's in the up-to-date API as fully supported, not deprecated, then it is not legacy.

Liutauras Vilda wrote:
Dave Tolls wrote:Raise a bug!


(and I have never been a fan of Scanner either)
But there is nothing wrong with Scanner. Just need to use it along with right tools instead of legacy ones.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have to agree Winston except for the fact that I get the very same problems when I move the source, and recompile, on my very stable box. Both boxes have Windows 10, and both were upgraded from Windows 8.1.

So for a bug report I would have to ask: for those that had the program run, did you do it on Windows 10 that was upgraded from 8.1, and were you on Java 8 current release, and did you try a file of trivial size, or more long the size of the one I am having problems with--209KB? And was it a native text file, or had it been converted from another format as my text file has been?

Winston Gutkowski wrote:
Les Morgan wrote:I have to agree that this is interesting. The articles that I have been reading are back from Java 7, so I have to assume that prior to Java 8, what I was doing not only worked, but was the norm. It seems that it has been broken in Java 8 though.

Hmmm. Seems a bit premature, particularly as you're using what you freely admit is a somewhat shaky version of Windows 10.

Is it possible that the OS has introduced something that makes Java behave different? (I wouldn't put it past MS to do that deliberately)

I think you'd have to try your original file (the one that was causing problems), with a few combinations of Java and OS version before you can say definitively where the problem lies.

Good luck though. And fun topic ... although maybe you don't think so.

Winston
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The number of threads that have turned up around Scanner and the issues with when a token is kept or not in the buffer.
Yes, multiple questions which arise from this matter gives an indication that documentation is not that well as Campbell pointed out earlier.

Les Morgan wrote:I would also have to disagree with your definition of "legacy". If it's in the up-to-date API as fully supported, not deprecated, then it is not legacy.
You have to disagree with Oracle here https://docs.oracle.com/javase/tutorial/essential/io/legacy.html

Oracle wrote:Prior to the Java SE 7 release, the java.io.File class was the mechanism used for file I/O, but it had several drawbacks.
Perhaps you have legacy code that uses java.io.File and would like to take advantage of the java.nio.file.Path functionality with minimal impact to your code.
The java.io.File class provides the toPath method, which converts an old style File instance to a java.nio.file.Path instance
Would be more beneficial if that part would be duplicated in API too rather than distributed to I/O tutorial only. And highly suggested not to use File along with Scanner in the first place since there is a nice way to convert File to Path.
But I look at it as about lack of explanation rather than considering as a bug. In fact, File class as Oracle states has bugs, that is a fact already.

And if to take seriously quote above, that means using FileReader/BufferedReader along with File can cause actual issues, which you might not aware about yet.

And as you found out yesterday Les, Scanner with Path read your file as expected, and at the same time pressumably you avoided possible drawbacks of File what Oracle mentioned there.

Worn out with my thoughts, just trying to be more proactive..
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do disagree with Oracle, for it is nothing more, nor less, than a breakdown in the commitment of excellence delivered by Sun. The code is in their active body of product and continues as relevant in their API, but now their commitment to it, while publishing it in their new API's, is lacking.

Les Morgan wrote:I would also have to disagree with your definition of "legacy". If it's in the up-to-date API as fully supported, not deprecated, then it is not legacy.
You have to disagree with Oracle here https://docs.oracle.com/javase/tutorial/essential/io/legacy.html

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!