• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer Problem

 
Anne Buena
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5804
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5804
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48935
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Don't use StringTokenizer. It's obsolete. Use a Scanner instead.

... or String.split() method.
 
Stephan van Hulst
Bartender
Pie
Posts: 5804
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be pretty cumbersome to load an entire file into a String before working on it
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48935
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic