aspose file tools*
The moose likes Beginning Java and the fly likes Scanner: troubles with hasNext() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner: troubles with hasNext()" Watch "Scanner: troubles with hasNext()" New topic
Author

Scanner: troubles with hasNext()

Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
This is my java code for testing the class "java.util.Scanner"

content of "file.txt"

N.B. There's a final space.
When i try to execute it, it says that after 3 there's a next.
Output:

When i remove the line for the delimiter,so by using the default (that is \s , so the spaces (included whitespace " " ) ), it appears:

Why 3 hasn't a next and in the previous yes? With "next" it mean the delimiter?
In the documentation there is wrote that hasNext() "returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method".
But in either case they haven't another Int number after! So can anyone explain why these results? Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

In the documentation there is wrote that hasNext() "returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method".

Aren't you misusing hasNext() instead of hasNextInt() ?


[My Blog]
All roads lead to JavaRanch
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Christophe Verré wrote:
In the documentation there is wrote that hasNext() "returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method".

Aren't you misusing hasNext() instead of hasNextInt() ?

Excuse-me, i mean hasNextInt(). With it it's ok.
But if i use for all the code hasNext() and next(), it returns the results that i've told previously. Why?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Is it possible your file has something else after the final space? Like a newline, maybe?

For clarity (for us), try putting the file contents directly in the demo program, like this:

That way there's no ambiguity about what's really in the file.

Compare with the results from this:
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Mike Simmons wrote:Is it possible your file has something else after the final space? Like a newline, maybe?

For clarity (for us), try putting the file contents directly in the demo program, like this:

That way there's no ambiguity about what's really in the file.

Compare with the results from this:

Then,i haven't only understand the second case. (with \n)
If the delimiter is " " then 3 haNext() is true , then this code:

returns
i.e. 3 has a space and after has the pattern (new line) and this last haven't a next.
By using the default delimiter it returns:

Why not the last (\n) pattern?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Harold Ferguson wrote:Why not the last (\n) pattern?

Well, because \n is whitespace. If your delimiter includes whitespace, as is the default case, then that \n will be included in the delimiter, and there's nothing after that final delimiter. If the delimiter is just " ", then the \n isn't part of the delimiter; it's the last token after the delimiter.

* Just in case: it's also worth noting that \n is not seen by the regex parser; instead, it sees a single newline character. So \n is not seen as a \ followed by n, but as one whitespace character. Probably you know that, but if not, it's worth mentioning.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
Also look at this old post about a related problem.
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Mike Simmons wrote:
Harold Ferguson wrote:Why not the last (\n) pattern?

Well, because \n is whitespace. If your delimiter includes whitespace, as is the default case, then that \n will be included in the delimiter, and there's nothing after that final delimiter. If the delimiter is just " ", then the \n isn't part of the delimiter; it's the last token after the delimiter.

* Just in case: it's also worth noting that \n is not seen by the regex parser; instead, it sees a single newline character. So \n is not seen as a \ followed by n, but as one whitespace character. Probably you know that, but if not, it's worth mentioning.

Also i thought like you. But the results are opposed. With " " , it checks the \n , with the default it doesn't check!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Harold Ferguson wrote:Also i thought like you. But the results are opposed. With " " , it checks the \n , with the default it doesn't check!

Other experts here will laugh, but this is one of the main reasons I hate Scanner. It's supposed to make life easier, but it doesn't, mainly because it doesn't shield you from vagaries of Stream processing, even for basic keyboard-based input.

In this case your input stream is a file, and it appears to be line-based; so my suggestion would be to either:
(a) Use hasNextLine()/nextLine() and then process each line independently.
(b) Use BufferedReader.readLine() (probably a fair bit faster, but that's NOT the reason to use it).

It might mean a bit more code, but I suspect you'll find the process a lot easier.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Harold Ferguson wrote:Also i thought like you. But the results are opposed. With " " , it checks the \n , with the default it doesn't check!

I'm not sure what you mean by "check" here, but the results are exactly what I would expect them to be. I'm not certain what you think they should be.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Winston Gutkowski wrote:Other experts here will laugh

HAH!!!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I have two objections to Scanner:

(a) The error-handling policy. Forcing people to check ioException() is a recipe for losing error info. I appreciate that they don't throw checked exceptions everywhere, but if they're going to follow that path, they should throw unchecked instead, like the rest of the modern world.

(b) The foolish decision to implement Iterator<String> rather than Iterable<String>. Clearly made by someone who did not understand how the foreach loop works.

Even with those annoyances, I find it a vast improvement over what we had before.
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Mike Simmons wrote:
Harold Ferguson wrote:Also i thought like you. But the results are opposed. With " " , it checks the \n , with the default it doesn't check!

I'm not sure what you mean by "check" here, but the results are exactly what I would expect them to be. I'm not certain what you think they should be.

Excuse-me, can you explain the "logic" of the output of the code?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I thought I just did.

With default delimiter:

First time though the loop: input is "1 2 3\n"

next() grabs the "1" as input, and the following " " is the delimeter. Prints "1 true".

Second time through the loop: the remaining (unconsumed) input is "2 3\n"

next() grabs the "2" as input, and the following " " is the delimeter. Prints "2 true".

Third time through the loop: the remaining (unconsumed) input is "3\n"

next() grabs the "3" as input, and the following " \n" is the delimeter. Because as I said earlier, "\n" is whitespace. Prints "3 false", because now that the final " \n" has been consumed as the delimiter, there's nothing left.

----

On the other hand, if we use the delimiter " ", we get the exact same results for the first two times through the loop. On the third time though the loop, the remaining (unconsumed) input is "3\n"

next() grabs the "3" as input, and the following " " is the delimeter, but not the "\n"

On the fourth time through the loop, the remaining (unconsumed) input is "\n"

next() grabs "\n", and there is nothing else for a final delimiter, so the scanner is done. Program prints "\n false" and exits.
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Excuse-me if i reply so late.
But if the input is "1 2 3\n" after the 3 there's no " " but only \n, so that you wrote i think it's wrong.
Anyway, i tried with the " " delimiter and the ouput is

"a" and "b" are clears. Why "c" doesn't print neither true nor false? Or maybe it take "c\n" and print "c" + a new line + false because it hasnt' a next?
Thanks for assistance.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Harold Ferguson wrote:Excuse-me if i reply so late.
But if the input is "1 2 3\n" after the 3 there's no " " but only \n, so that you wrote i think it's wrong.

Well,it's completely true for the input I described. That was slightly different for the input I described earlier - sorry about that. You never really described the input, as far as a final " " or "\n", so I'm just guessing anyway.
Harold Ferguson wrote:Why "c" doesn't print neither true nor false?

But it does. That final "false" doesn't get there by magic.
Harold Ferguson wrote:Or maybe it take "c\n" and print "c" + a new line + false because it hasnt' a next?

Exactly.
Harold Ferguson
Greenhorn

Joined: Sep 05, 2012
Posts: 12
Ok. Thanks! Now i've understand .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner: troubles with hasNext()