File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Scanner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner question" Watch "Scanner question" New topic

Scanner question

John Powell

Joined: Nov 13, 2004
Posts: 11

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

Joined: Dec 06, 2001
Posts: 3061
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 Perhaps printing out the value returned by next() will help see what is going on.



Java API Documentation
The Java Tutorial
John Powell

Joined: Nov 13, 2004
Posts: 11
12/25/1975 -> it displays:

... 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?

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 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)
I agree. Here's the link:
subject: Scanner question
It's not a secret anymore!