如何让在一行中输入逗号分隔的数字到Java中的数组 [英] how to get numbers separated by comma entered in a line into an array in Java

查看:703
本文介绍了如何让在一行中输入逗号分隔的数字到Java中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能得到用户EQ的一行中输入值:1,3,400,444,等到一个数组。我知道我必须在此情况下,宣布一个分隔逗号,。可能有人帮助

感谢


解决方案

 字符串输入=1,3,400,444;
的String []号= input.split(\\\\ S *,\\\\ S *);

您可以用更简单的分隔符<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29\"><$c$c>String.split(),但更复杂的\\\\ S *,\\\\ S *另外去掉逗号周围的空格

how could I get values entered in a single line by the user eq: 1, 3, 400, 444, etc.. into an array. I know I must declare a separator in this case the comma ",". Could someone help

Thanks

解决方案

String input = "1, 3, 400, 444";
String[] numbers = input.split("\\s*,\\s*");

You can use much simpler separator in String.split() like "," but the more complex "\\s*,\\s*" additionally strips whitespaces around comma.

这篇关于如何让在一行中输入逗号分隔的数字到Java中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆