Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Scanner to read a single character  RSS feed

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new Scanner class has no getChar method. Is there a preferred way to read a single character using the Scanner class?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if you can get next int and cast it to char.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, won't nextInt() throw an InputMismatchException if the next token isn't a text representation of a number? It's like calling Integer.parseInt("foo") - there's no way to interpret that as a number.

For what Barry wants - well, I'm actually a bit surprised there's no direct method for this. Hunh. I don't really see any good answer desides the obvious: Call scanner.next() or scanner.nextLine() to get a String, then use charAt() to get whatever chars you want. Not great, but I don't see a better option.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think nextInt will go even further wrong - won't it try to read 4 bytes? How strange they left read one char out of scanner.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
won't it try to read 4 bytes?

No, it's treating everything as text, not bytes, and will try to read everything up to the next delimiter (whatever the delimiter is currently set to) and try to interpret that text as an int. So if the next token is "1234567890" that will be interpreted as the int 1234567890. (However many bytes that is in the default encoding - probably 0)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. I was thinking it would try to parse binary into an int, but that's not the case. Never mind
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a clever way to use delimiters to get the next single character from the input?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a matter of fact:

This uses positive lookbehind - a delimiter can be found at any position from which you can look back and find a single character. The key is that each time the scanner consumes a token with next(), that token can no longer be seen by the scanner when scanning subsequent input. So lookbehind doesn't see any of the chars that have already been consumed; it only matches and finds a delimiter when there's at least one new character behind the delimiter.
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option:

It's really too bad Scanner implements Iterator<String> but not Iterable<String>. Then we could code this:

Maybe Sun could extend the new foreach to work directly on Iterators as well as on Iterables. I can't see any downside to this. Unless/until that happens though, the moral is: don't implement Iterator except as part of an implementation of Iterable. That way, people can easily use your class in a for loop.

Hurm, in a similar vein it would be nice if a CharSequence were Iterable<Character> as well. Then we could do

without making an extra toCharArray(). Oh well...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!