Java - How to split a string with numbers separated by spaces into variables?

后端 未结 1 1744
醉酒成梦
醉酒成梦 2021-01-17 09:16

I am given the string \"23 45\" I need to get 2 variables (int a) with the value 23 and (int b) with value 45

相关标签:
1条回答
  • 2021-01-17 09:54

    if your input String name is ' in ' , then we will have :

    String in = "23 45";
    
    String s[] = in.split(" "); 
    
    int out[] = new int[s.length]; 
    
    for(int i = 0 ; i < s.length ; i++) 
    
         out[i] = Integer.parseInt(s[i]);
    

    output is now in out array

    0 讨论(0)
提交回复
热议问题