Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# how to extract two numbers out of a string?

Posted on 2004-10-26
Medium Priority
244 Views
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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4
• 2
• +1

LVL 86

Expert Comment

ID: 12414677
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

ID: 12414823
Sorry - noticed a few errors there - back later
0

LVL 13

Expert Comment

ID: 12414849
What JDK version r u using?
0

Author Comment

ID: 12414927
JDK 1.4
0

LVL 3

Expert Comment

ID: 12415031

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

LVL 3

Expert Comment

ID: 12415136

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 Comment

ID: 12415256
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

ID: 12415571

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

LVL 86

Accepted Solution

CEHJ earned 280 total points
ID: 12415591
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

ID: 12415883

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

0

LVL 3

Expert Comment

ID: 12415887

(Just out of sheer curiousity) :)

0

LVL 13

Expert Comment

ID: 12415897

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

LVL 86

Expert Comment

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

Yes
0

LVL 3

Expert Comment

ID: 12415983

Heh cool.

Everyday I learn something new.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET â€¦
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula wâ€¦
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â€¦
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
###### Suggested Courses
Course of the Month8 days, 7 hours left to enroll