• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner question

 
John Powell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to break apart a string using the Scanner class, then display each string to the console. When I enter in the date 12/15/1975 it just hangs without displaying anything. Any ideas?


 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with the Scanner class as it is brand new to Java 5.0. However, I have a few questions and comments that may help you figure out the problem. First, do you know where it hangs? Does it hang up when you try to get the day, month, or year? If you aren't sure, perhaps you can add some System.out.println() calls between each call to kdb.next(). Perhaps printing out the value returned by next() will help see what is going on.

HTH

Layne
 
John Powell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
12/25/1975 -> it displays:

12
25
... it hangs on the year. It think it has something to do with the delimiter (/). It doesn't have one at the end of the year so it just hangs. I don't know how you get around the problem. Any ideas?

Thanks
John
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's waiting for you to enter the next delimeter or for the stream to end. You're better off using a Reader wrapped around System.in and Reader.readLine() to read the input. Then use the Scanner on the line you read in. Since the String will have an end, the scanner should break it apart correctly.

Otherwise, if you're using a version of Unix/Linux, I believe you can tell it the stream has ended using control-D. I have no idea how to do that on Windows . . . perhaps control-alt-delete. (No, don't do that)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic