0

I have a string:

"3, V, 11, H, 21, H"

and I am trying to get

int first = 3
int second = 11
int third = 21

I'm not exactly sure how to do this since the numbers could be one or two digits, there are non-digit characters between the numbers, and I have to capture multiple numbers. I tried regex but then I'm left with "31121" which does not indicate what the three numbers are.

1
  • I tried regex but then I'm left with "31121" Can we see your code that generates this output? Also it seems that your data are in format number, data, number, data, number, data,.... In that case you can just split your input on comma and parse every even indexed elements to integers. Commented Dec 15, 2013 at 19:50

4 Answers 4

1

Try this code. Should get you the job done.

public static void main(String[] args){
    String s = "3, V, 11, H, 21, H";
    String[] t = s.split(" [ ,]*|,[ ,]*");
    int first = Integer.parseInt(t[0]);
    int second = Integer.parseInt(t[2]);
    int third = Integer.parseInt(t[4]);
    System.out.println(first);
    System.out.println(second);
    System.out.println(third);
}
Sign up to request clarification or add additional context in comments.

4 Comments

you should use .trim(). I think t[2] would be " 11"
@mikeyaworski no, it's not
Oh because you split it with the space included. Well it'll work, but only under these perfectly strict conditions.
@mikeyaworski just made it much better
1

You can split your String by "," and check if it's a number using NumberUtils.isNumber (String str) from org.apache.commons.lang.math.NumberUtils :

Checks whether the String a valid Java number.

Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).

Null and empty String will return false.

String s = "3, V, 11, H, 21, H";
for(String st : s.split(",")){
    if(NumberUtils.isNumber(st.trim()))
        System.out.println(st);
}

If you want to check that the String contains only digits, you can use NumberUtils.isDigits(String str)

Comments

1
public static void main(String[] args) {

    String in = "3, V, 11, H, 21, H";

    List<String> storage = Arrays.asList(in.split(","));
    List<Integer> output = new ArrayList<Integer>();

    int first = 0;
    int second = 0;
    int third = 0; 

    for(String str : storage){
        if(str.trim().matches("[0-9]+") ){ // or if(NumberUtils.isNumber(str) )
            output.add(Integer.parseInt(str.trim()));
        }
    }

    if(output.size() == 3){
         first = output.get(0);
         second = output.get(1);
         third = output.get(2);
    }

    System.out.print("first: "); System.out.println(first);
    System.out.print("second: "); System.out.println(second);
    System.out.print("third: "); System.out.println(third);

}

Output:

first: 3
second: 11
third: 21

Comments

0

You can split this string by , and then check if every part is a number like this :

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        String str = "3, V, 11, H, 21, H";
        String[] parts = str.split(", ");
        ArrayList<Integer> listNumbers = new ArrayList<Integer>();
        for(String x : parts){
         try{
           listNumbers.add( Integer.parseInt(x) );  
         }
         catch(Exception e){}    
        }

        for(int i=0;i<listNumbers.size();i++) System.out.println("Number "+(i+1)+" : "+listNumbers.get(i));
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.