# how to extract two numbers out of a string?

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

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Commented:
Sorry - noticed a few errors there - back later
0
Commented:
What JDK version r u using?
0
Author Commented:
JDK 1.4
0
Commented:

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

0
Commented:

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);
}

0
Author Commented:
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
Commented:

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?

0
Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:

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

0
Commented:

(Just out of sheer curiousity) :)

0
Commented:

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]) );
}
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
Commented:
>>CEHJ, would that parse the 2e-004 typed numbers too?

Yes
0
Commented:

Heh cool.

Everyday I learn something new.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.