aspose file tools*
The moose likes Java in General and the fly likes Extracting integers from strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting integers from strings" Watch "Extracting integers from strings" New topic
Author

Extracting integers from strings

E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
I am reading data from a file, each line has a certain number of integers. They are seperated by whitespace. When I read them in I use the readLine() method which reads the lines in as a string. What I am trying to do is seperate each of these integers so that I can compare them to other integers on other lines. How do I seperate the integers seeing that they are all in a single string? This is the code I have tried to use but havent had any luck as of yet:
String maxVals = in.readLine();
maxVals = maxVals.trim();
System.out.println("Max values are: " + maxVals);
int i = 0;
char buff = maxVals.charAt(i);
System.out.println("buff is: " + buff);
i++;

while (buff.compareTo(' ') > 0){
buff += maxVals.charAt(i);
i++;}
I get an error saying that char cannot be dereferenced. I am not even sure if this way will work because some of the integers are more than on digit. When I add them together I won't get a 10 instead I will get 'a'. How do I extract integers that are more than 1 digit?
Thanks
EF
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use the java.util.StringTokenizer class to break up the string.


[Jess in Action][AskingGoodQuestions]
Don Morgan
Ranch Hand

Joined: Jul 24, 2003
Posts: 84
I am new to javaranch, but do have some experience with java.
Anyway, here's an example using the tokenizer to pickup the integers separated by whitespace.

From the javadocs, the default delimiter is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.
Cheers,
Don


Don Morgan, Founder
www.DeveloperAdvantage.com - FREE Audiobooks for Software Developers
E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
are these values that are returned char or int? also if I use the string tokenizer on 2 strings right after each other how do I compare them to each other?
Thanks
[ July 24, 2003: Message edited by: E Fox ]
I figured it out, thanks for the help.
[ July 24, 2003: Message edited by: E Fox ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

StringTokenizer.nextToken() returns String; each token will be a String. If you want to convert them to integers, use the static method Integer.parseInt(String), which returns int.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
You may find this thread helpful.
Regards,
Ellen
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I suggest learning and using regular expressions instead of the (now pointless) StringTokenizer class. The .split() method in String works just dandy:

The '\s' regular expression matches whitespace. It is double backslashed because otherwise teh compiler will complain about an illegal escape sequence....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
One comment on style :roll:
I much prefer

Over

Also, you may want to add a '+' sign after the \s. The '+' is a quantifier which says match the preceding expression one through infinity. This will gobble up successive whitespace.
Another thing you may want to consider is to use the regex package and match the numbers explicitly with \d. Then with the help of capturing parenthesis you can collect your numbers easily.
Cheers,
Leslie


Normal is in the eye of the beholder
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Joel,
wouldn't "[\\s]+" eliminate the need for checking for empty Strings? ( parses values that are separated by one or more whitespaces )

[ July 25, 2003: Message edited by: Jamie Robertson ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Extracting integers from strings