Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting integers from strings

 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the java.util.StringTokenizer class to break up the string.
 
Don Morgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find this thread helpful.
Regards,
Ellen
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic