extracting integers from string

Discussion in 'Java' started by R, Jul 25, 2003.

  1. R

    R Guest

    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
    R, Jul 25, 2003
    #1
    1. Advertising

  2. "R" <>, 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);
    }
    catch (NumberFormatException notAnInteger)
    {
    // do something, log it or whatever.
    }
    }


    greets
    bhun
    dhek bhun kho, Jul 25, 2003
    #2
    1. Advertising

  3. R

    Jon A. Cruz Guest

    Jon A. Cruz, Jul 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    957
    Malte
    Mar 3, 2005
  2. Mike Vallely

    integers as a string

    Mike Vallely, Nov 10, 2003, in forum: C++
    Replies:
    3
    Views:
    407
    Tim Threlfall
    Nov 10, 2003
  3. Chris Schumacher

    string lengths as integers.

    Chris Schumacher, Nov 14, 2003, in forum: C++
    Replies:
    3
    Views:
    459
    Catalin Pitis
    Nov 14, 2003
  4. Bob Greschke

    Extracting 3-byte integers

    Bob Greschke, Jun 27, 2006, in forum: Python
    Replies:
    4
    Views:
    645
    Bob Greschke
    Jun 27, 2006
  5. dor
    Replies:
    4
    Views:
    322
    Daniel T.
    Mar 14, 2006
Loading...

Share This Page