File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String Tokenizer Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer Problem" Watch "String Tokenizer Problem" New topic
Author

String Tokenizer Problem

Anne Buena
Greenhorn

Joined: Mar 11, 2012
Posts: 14

Hi! I have some confusion with StringTokenizer. I have this line of text in file:

1012012*8:00*12:00*

The problem is this file is being updated. For short, the number of tokens is unknown because it is updated always.
What code should I use? Do I need to use array or what?

Thank you for the response
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

We can't really give you an answer. What's the format of the data? How will it be updated? What information do you want to extract? In short, TellTheDetails.

I can already give you one tip. Don't use StringTokenizer. It's obsolete. Use a Scanner instead.
Anne Buena
Greenhorn

Joined: Mar 11, 2012
Posts: 14

The data is int.

1012012*8:00*12:00*

I want to get the first token then the second and so on for computation. My only problem is that I don't know what action should I do if the number of tokens are not defined or unknown.

Thank you for that tip. What should be my algorithm/steps for scanner?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Obviously the format is not int, because there are characters like '*' and ':'. How should we deal with them?

Anyway, if you know how to delimit the input, then you can create a Scanner instance on the input, and set the delimiter. Then all you have to do is perform a while loop in which you check Scanner.hasNextInt() and in the body you read Scanner.nextInt().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38794
    
  23
Anne Buena wrote:The data is int. . . .
No, it isn’t. I can see no way to get an int out of that expression, not even if : means divide. That is a String with various numbers in. It should be easy enough to work out a regular expression for, but beware ^ is a meta-character in regular expressions.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 507
Stephan van Hulst wrote:Don't use StringTokenizer. It's obsolete. Use a Scanner instead.

... or String.split() method.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

I think it would be pretty cumbersome to load an entire file into a String before working on it
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 507
Stephan van Hulst wrote:I think it would be pretty cumbersome to load an entire file into a String before working on it

Actually, I read the text file line by line (using BufferedReader & FileReader) in while loop and split contents of each line using split method. I have not tried Scanner yet.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38794
    
  23
A Scanner only works on a text file. It is easier than a buffered reader. If you click the link on the top line, you will find code similar to this:
 
wood burning stoves
 
subject: String Tokenizer Problem