-3

I have got a string "1,2,3" and I need to split it and individual numbers convert to integer.

String s = "1,2,3";
String[] s2 = s.split(",");
for(String temp : s2 ) {
System.out.println(temp);
}

I know to split it but i don't know to convert it. Sorry for my bad English. Thank you!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Maco
  • 9

1 Answers1

2

You can always use Integer.parseInt(...) be aware abut the NumberFormatException

Example:

public static void main(String[] args) {

    String s = "1,2,s,3";
    String[] s2 = s.split(",");
    int var = 0;
    for (String temp : s2) {
        try{
            var = Integer.parseInt(temp);
        }catch(NumberFormatException ex){
            System.err.println("Oops... that wasn't a number....");
        }finally{
            System.out.println(temp);
        }
    }
}
dnault
  • 8,340
  • 1
  • 34
  • 53
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97