Grabbing the integer out from a string...

G

ganggang3ster

Is there a method in String, Integer, or other classes that will
return the
integer within a string like:

int a = ClassName.method("I have 7 peanuts");
System.out.print(a);

Output:
7

I have an uneasy feeling that the answer may be no, and my current
solution uses regular expressions :(

Thanks!
 
L

Lew

Eric said:
     As far as I know, your uneasy feeling is justified.  I'd
probably use a regular expression, too, but I'd also worry about

        "I have 7,240 peanuts"
        "I have 7 240 peanuts"
        "I have 7.24 kilopeanuts"
        "I have seven peanuts"
        "I have VII peanuts"
        "I have 7 peanuts, 8 elephants, and a problem"

"The clip holds 10 9-mm rounds."
 
L

Lew

Eric said:
=A0 =A0 =A0As far as I know, your uneasy feeling is justified. =A0I'd
probably use a regular expression, too, but I'd also worry about

=A0 =A0 =A0 =A0 "I have 7,240 peanuts"
=A0 =A0 =A0 =A0 "I have 7 240 peanuts"
=A0 =A0 =A0 =A0 "I have 7.24 kilopeanuts"
=A0 =A0 =A0 =A0 "I have seven peanuts"
=A0 =A0 =A0 =A0 "I have VII peanuts"
=A0 =A0 =A0 =A0 "I have 7 peanuts, 8 elephants, and a problem"

"The clip holds 10 9-mm rounds."

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I am not aware of any evidence showing that President Bush
or members of his administration have personally profited
from the attacks of 9-11"

--- Rep. Cynthia McKinney, D-Ga.
 
S

Stefan Ram

@gmail.com said:
int a = ClassName.method("I have 7 peanuts");

public class Main
{
public static void main( final java.lang.String[] args )
{
final int a = new java.util.Scanner( "I have 7 peanuts" ).
skip( "[^\\d]+" ).nextInt();

java.lang.System.out.println( a ); }}

7
 
L

Lew

@gmail.com said:
int a = ClassName.method("I have 7 peanuts");

public class Main
{
  public static void main( final java.lang.String[] args )
  {
    final int a = new java.util.Scanner( "I have 7 peanuts" ).
    skip( "[^\\d]+" ).nextInt();

    java.lang.System.out.println( a ); }}

7

"I flew on the 1,000th 747 and an F-16 200 times for under 100 Euros
by calling +011 14 987662 1,000 times on 2006-06-06 and answering the
sixth question, 'What is 222 times 3?'"

How does one know to pick up "1,000" as the int "1000"? That "747"
does not actually represent an int but a word? That "16" is not
separate but part of "F-16"? That "2006-06-06" represents a date, not
an int? That "sixth" does represent an int? That "222 times 3"
really represents a single int?
 
S

Stefan Ram

D

Danger_Duck

Is there a method in String, Integer, or other classes that will
return the
integer within a string like:

int a = ClassName.method("I have 7 peanuts");
System.out.print(a);

Output:
7

I have an uneasy feeling that the answer may be no, and my current
solution uses regular expressions :(

Thanks!

Thanks guys.

And Lew, obviously I'm not applying this to everything-I have output
from code that always comes in a predictable form like "function f1
occurred 5 times 100% of blocks executed"

So, as I read line by line of basically the same thing over and over
again (with the two numbers and function name changing), I naturally
wanted to know if there was an easy way to pick out the integers or
split it up at the first int if 2 or more numbers in a string would
make things confusing.

By the way Stefan, is it possible to use regular expressions to do
something like line.substring(line.indexOf(FIRST NUMERICAL VALUE)) ?

Again, often times I may want to split up strings that occur regularly
like the one above but don't care particularly for the exact number,
though I know the pattern is always the same.

Thanks again!
 
A

Andrew Thompson

So, as I read line by line of basically the same thing over and over
again (with the two numbers and function name changing), I naturally
wanted to know if there was an easy way to pick out the integers or
split it up at the first int if 2 or more numbers in a string would
make things confusing.

<sscce>
class FirstInt {
public static void main(String[] args) {
String s = args[0];
String[] parts = s.split(" ");
for (int ii=0; ii<parts.length; ii++) {
try {
Integer i = new Integer(parts[ii]);
System.out.println(i);
break;
} catch(Exception e) {
// continue
}
}
}
}
</sscce>

[IP]
java FirstInt
java FirstInt "I have 7 peanuts"
java FirstInt "I have 21 peanuts in 3 pockets"
[/IP]

[OP]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at FirstInt.main(FirstInt.java:3)
7
21
[/OP]
 
J

Jussi Piitulainen

Danger_Duck said:
from code that always comes in a predictable form like "function f1
occurred 5 times 100% of blocks executed"

So, as I read line by line of basically the same thing over and over
again (with the two numbers and function name changing), I naturally
wanted to know if there was an easy way to pick out the integers or
split it up at the first int if 2 or more numbers in a string would
make things confusing.

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class M {
public static void main(String [] args) {
Pattern p
= Pattern.compile("function (\\w+)"
+ " occurred (\\d+) times"
+ " (\\d+)% of blocks executed");
for (int k = 0 ; k < args.length ; ++ k) {
Matcher m = p.matcher(args[k]);
if (m.matches()) {
System.out.println("group 1 == |" + m.group(1) + "|");
System.out.println("group 2 == |" + m.group(2) + "|");
System.out.println("group 3 == |" + m.group(3) + "|");
}
}
}
}

$ java -cp . M "function foo03_1 occurred 704 times 39% of blocks executed"
group 1 == |foo03_1|
group 2 == |704|
group 3 == |39|
 
T

Tom Anderson

"The clip holds 10 9-mm rounds."

"I am l33t."

Anyway, it's time for my favourite Jamie Zawinski quote:

"Some people, when confronted with a problem, think 'I know, Ill use
regular expressions'. Now they have two problems."

tom
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top