Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > extracting integers from string

Reply
Thread Tools

extracting integers from string

 
 
R
Guest
Posts: n/a
 
      07-25-2003
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



 
Reply With Quote
 
 
 
 
dhek bhun kho
Guest
Posts: n/a
 
      07-25-2003
"R" <(E-Mail Removed)>, Fri, 25 Jul 2003 00:08:58 +0000:

> 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?


I would rather use regular expressions to do it, this assumes the integers
are separated by spaces:


String[] parts = in.readLine().split(" ");
for (int i = 0; i < parts.length; i++)
{
try
{
Integer integer = new Integer(parts[i]);
}
catch (NumberFormatException notAnInteger)
{
// do something, log it or whatever.
}
}


greets
bhun




 
Reply With Quote
 
 
 
 
Jon A. Cruz
Guest
Posts: n/a
 
      07-25-2003
Ken Ream wrote:
> http://java.sun.com/j2se/1.4.1/docs/api/
> Take a look at StringTokenizer


And java.text.MessageFormat

Might want to use them together.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting 3-byte integers Bob Greschke Python 4 06-27-2006 05:46 PM
extracting integers from a string. dor C++ 4 03-14-2006 02:39 PM
XSLT - Extract leading integers from text string grghoward@no.spam.email.com.no.spam XML 4 03-03-2005 02:41 PM
string lengths as integers. Chris Schumacher C++ 3 11-14-2003 06:30 AM
integers as a string Mike Vallely C++ 3 11-10-2003 10:21 PM



Advertisments