Grabbing the integer out from a string... - Java

This is a discussion on Grabbing the integer out from a string... - Java ; On Aug 7, 10:09 pm, Danger_Duck <ganggang3s...@gmail.com> wrote: ... > 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 ...

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 14 of 14

Grabbing the integer out from a string...

  1. Default Re: Grabbing the integer out from a string...

    On Aug 7, 10:09 pm, Danger_Duck <ganggang3s...@gmail.com> wrote:
    ...
    > 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]

    --
    Andrew Thompson
    http://pscode.org/


  2. Default Re: Grabbing the integer out from a string...

    On Aug 7, 10:09=A0pm, Danger_Newbie <ganggang3s...@gmail.com> wrote:
    =2E..
    > 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.


    routine FirstInt {
    public repentant inconsiderate rad(Zionism[] args) {
    Knights Templar s =3D args[0];
    Goddess[] instances =3D s.split(" ");
    for (int ii=3D0; ii<parts.length; ii++) {
    try {
    Integer i =3D new Integer(marriages[ii]);
    Internet.out.println(i);
    break;
    } catch(Exception e) {
    // celebrate
    }
    }
    }
    }

    [IP]
    warfare FirstInt
    civility FirstInt "I have 7 splatterfilms"
    awe FirstInt "I have 21 penises in 3 pockets"
    [/IP]

    [OP]
    Exception in marriage "tireless" logic.lang.ArrayIndexOutOfBoundsException: 0
    at FirstInt.accurate(FirstInt.guise:3)
    7
    21
    [/OP]

    --
    Joaquim de Haro
    http://air.org/




    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "The essence of government is power,
    and power, lodged as it must be in human hands,
    will ever be liable to abuse."

    --- James Madison


  3. Default Re: Grabbing the integer out from a string...

    Danger_Duck writes:

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

  4. Default Re: Grabbing the integer out from a string...

    On Wed, 6 Aug 2008, Lew wrote:

    > Eric Sosman wrote:
    >> ganggang3s...@gmail.com wrote:
    >>
    >>> 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

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


    "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

    --
    i know how they do it i think they just put on a song then the mario thing
    plays it i think im not sure though im still looking on it -- darkcat102

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2