Solved

how to extract two numbers out of a string?

Posted on 2004-10-26
202 Views
Last Modified: 2010-03-31
experts,

I have a string like this
 "gnl|CDD|2138 pfam01601, Corona_S2, Coronavirus S2 glycoprotein. ...    38   0.002"
How to extract the last two numbers from this string? expecially the last number "0.002"?

0
Question by:dodowoman
    14 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Try

    Pattern p = Pattern.compile(".+(\\d+) ([\\d\\.]+)";
    String s = "gnl|CDD|2138 pfam01601, Corona_S2, Coronavirus S2 glycoprotein. ...    38   0.002";
    Matcher m = p.matcher(s);
    if (m.matches()) {
        String integer = s.group(1);
        String decimal = s.group(2);
    }

    You need to import java.util.regex.*;
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Sorry - noticed a few errors there - back later
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    What JDK version r u using?
    0
     

    Author Comment

    by:dodowoman
    JDK 1.4
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    What might be the syntax of the last two numbers? Should we assume the first one is an integer and the second a float?

    Aviad.
    0
     
    LVL 3

    Expert Comment

    by:aviadbd


    To improve on CEHJ solution a bit:

                    String s = "gnl|CDD|2138 pfam01601, Corona_S2, Coronavirus S2 glycoprotein. ...    38   0.002";
                    Pattern p = Pattern.compile(".+\\s+(\\d+)\\s+([\\d\\.]+)$");

                    Matcher m = p.matcher(s);
                    if (m.matches()) {
                            int i = Integer.parseInt(m.group(1));
                            float f = Float.parseFloat(m.group(2));
                            System.out.println("i = " + i + ", f = " + f);
                    }

    AviadBD.
    0
     

    Author Comment

    by:dodowoman
    yes, the first number is "usually" integer, or double (like 33.4), and the second number could be "2e-004" or "4.003" type of float
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    Oh my. :)

    So first number is either an integer or float, but Not 2e-004 for this matter, and second number is either 2e-004 kind of format or a float?

    Another question, what is the exact format of the 2e-?? type?

    Aviad.
    0
     
    LVL 86

    Accepted Solution

    by:
    You could do

    String s = "gnl|CDD|2138 pfam01601, Corona_S2, Coronavirus S2 glycoprotein. ...    38   0.002";
    String[] atoms = s.split("\\s+");
    int i = Integer.parseInt(atoms[atoms.length - 2]);
    double d = Double.parseDouble(atoms[atoms.length - 1]);
    System.out.println(i);
    System.out.println(d);
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    CEHJ, would that parse the 2e-004 typed numbers too?

    AviadBd.
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    (Just out of sheer curiousity) :)

    Aviad
    0
     
    LVL 13

    Expert Comment

    by:petmagdy

    code this function:

      public Vector getDoublesInString(String containsDoubles) throws Exception
      {
        String allowedChars = "0123456789.";
        String tested = new String(containsDoubles);
        Vector discoveredDoubles = new Vector();
        for(int i=0; i < tested.length() ; i++)
        {
          char aChar = tested.charAt(i);
          if( allowedChars.lastIndexOf(aChar) == -1 )
          {
            tested=tested.replace(aChar, ' ');
          }
        }
        String[] candidateNumbers = tested.split("[ ]+");
        for(int i=0 ; i < candidateNumbers.length; i++)
        {
          try
          {
            Double dblValue = new Double( Double.parseDouble(candidateNumbers[i]) );
            discoveredDoubles.add(dblValue);
          }
          catch(NumberFormatException nfe)
          {
            //Not double
          }
        }
        return discoveredDoubles;
      }


    and called like this:

        try
        {
          Vector doubles =  class1.getDoublesInString("gnl|CDD|2138 pfam01601, Corona_S2, Coronavirus S2 glycoprotein. ...    38   0.002");
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
     
    take the last 2 elements in the vector

    Good Luck :)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>CEHJ, would that parse the 2e-004 typed numbers too?

    Yes
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    Heh cool.

    Everyday I learn something new.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This video teaches viewers about errors in exception handling.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now