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

拟墨画扇 提交于 2020-01-30 04:04:36

问题


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:


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



来源:https://stackoverflow.com/questions/39882367/java-how-to-split-a-string-with-numbers-separated-by-spaces-into-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!