Okay, so I have a text file like this:

I Did It Your Way, 11.95
The History of Scotland, 14.50
Learn Calculus in One Day, 29.95
Feel the Stress, 18.50
Great Poems, 12.95
Europe on a Shoestring, 10.95
The Life of Mozart, 14.50

And, as you can see, there are prices of the items at the end. Somehow I want to extract thsee numbers and read them into an array so I can perform calculations with them... What could I possibly do? Thanks!

Re: Should I use the String Tokenizer class for this? 80 80

The StringTokenizer class is the old way of getting tokens from a String.
The new way is the String class's split() method. However using split() requires you to use regular expressions which can take a while to master.

Re: Should I use the String Tokenizer class for this? 80 80

So... how would I get these numbers? I've been struggling developing methods to try and do this... Nothing has worked fully yet :/

Re: Should I use the String Tokenizer class for this? 80 80

If you split on the comma each line will give you a two element array. The first element will be the name and second will be the price. It's a String array, so you will then need to parse the price from String to double (or whatever) - the Double class has a method to do that.

Re: Should I use the String Tokenizer class for this? 80 80

NormR1 is right. the split() method is what you should use, but I'm not sure why he suggests regex here. if you use the comma as separator split(",");, the way JamesCherrill suggests it, you have what you need in the second element of the resulting array. (which yes, you still need to parse to a numerical type, but JamesCherrill already told you how to do that.)

the only thing to keep in mind, is that you'll have to keep the comma as a separator for future lines, and you're not allowed to add titles which have comma('s) in them.

for instance, if you would add,

I, Claudius, 12.35

the price would not be in the second element of the array, but in the third.

you can handle this kind of issue like this:

`    String[] info = bookInfo.split(",");`
     String price = bookInfo[bookInfo.length-1];
     // parse price to a numerical type
Re: Should I use the String Tokenizer class for this? 80 80

I'm not sure why he suggests regex here

There's no choice, the API doc for String split specifies that the param is a regex.

public String[] split(String regex)
Splits this string around matches of the given regular expression.

fortunately ',' isn't a metachar, so you can split on "," and it works.

Re: Should I use the String Tokenizer class for this? 80 80
       String[] anArray = "Europe on a Shoestring, 10.95".split(",");
       System.out.println("anArray=" + Arrays.toString(anArray));  // anArray=[Europe on a Shoestring,  10.95]
       System.out.println(">" + anArray[1] + "<");   // > 10.95<

The reason I said a regular expression is required is shown above. The space after the , is included in the second element in the array. The simple regexp: "," leaves the space. To get only the tokens requires a more sophisticated regexp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.