[英]How to take numbers from a string and put them into an arrayList
I have a program that I am trying to take a set of numbers from a string separated by commas and place them into an ArrayList
; 我有一个程序,我试图从逗号分隔的字符串中取一组数字,并将它们放入
ArrayList
; however, I'm not quite sure how to do it. 但是,我不太清楚该怎么做。 So far what I have done is turn the String into an array of chars and then convert the chars into ints by using:
到目前为止,我所做的是将String转换为一个字符数组,然后使用以下方法将字符转换为整数:
Character.digit(temp[i], 10)
This example is in a for loop iterating over a string. 此示例在for循环中迭代字符串。 Let's say in this case
"1,2,3,4"
. 让我们说在这种情况下
"1,2,3,4"
。 taking the first element of the new char array and converting it to a int. 获取新char数组的第一个元素并将其转换为int。
My issue is, 我的问题是,
"34,2,3,65,125"
. "34,2,3,65,125"
。 these will be stored as separate elements of the array when i need it to be one element. "-123,45,3,4,-6"
. "-123,45,3,4,-6"
。 Remember that this is mean to be for any String
argument. 请记住,这对于任何
String
参数都是String
。
There are lots of conditions here and I'm not sure how to solve them. 这里有很多条件,我不知道如何解决它们。
Consider using 考虑使用
String.split()
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html String.split()
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html Integer.parseInt()
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer.parseInt()
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Regarding "any String argument," you have a choice: either fail on the strings which are not comma-separated numbers or redefine the task. 关于“任何String参数”,您可以选择:对不是以逗号分隔的数字的字符串进行失败或重新定义任务。 Here comes the essence of the programming: you need to define everything.
这就是编程的本质:你需要定义一切。 The easiest way (and the safest, usually) is to fail whenever you see something unexpected.
最简单的方法(也是最安全的方法)是每当你看到意想不到的东西时都会失败。 Java will do it for you in this case, so enjoy.
在这种情况下,Java会为你做,所以尽情享受吧。
you could just do: 你可以这样做:
String input = "-12,23,123123";
String[] numbers = input.split(",");
List<Integer> result = new ArrayList<Integer>();
for(String number : numbers){
result.add(Integer.parseInt(number));
}
Use the String.split() function to break your String in different strings, based on the separator: 使用String.split()函数根据分隔符在不同的字符串中断开String:
String input="-123,45,3,4,-6";
String[] vals=input.split(",");
List<Integer> numbers=new ArrayList<Integer>(vals.length);
for(String val:vals) {
numbers.add(Integer.valueOf(val));
}
First split the input String
using String.split()
. 首先使用
String.split()
拆分输入String
。 Then try Integer.parseInt()
. 然后尝试
Integer.parseInt()
。
String testStr = "123,456,789";
String tokens[] = testStr.split(",");
ArrayList<Integer> numList = new ArrayList<Integer>();
for(int i = 0; i < tokens.length; i++)
{
try
{
Integer num = new Integer(tokens[i]);
numList.add(num);
}
catch(NumberFormatException e)
{
//handle exception
}
}
String input="-123,45,3,4,-6, a";
String[] vals=input.split(",");
List<Integer> numbers=new ArrayList<Integer>(vals.length);
for(String val:vals) {
try {
int a = Integer.valueOf(val);
numbers.add(a);
} catch (NumberFormatException e) {
// TODO: handle exception
}
}
Use this code. 使用此代码。 By using it you can also avoid non-integer values if there any.
通过使用它,您还可以避免非整数值(如果有)。
您还可以使用StringTokenizer将字符串拆分为子字符串,然后使用Integer.parseInt将它们转换为整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.