简体   繁体   English

如何从字符串中取数字并将它们放入arrayList中

[英]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, 我的问题是,

  • A: there has to be a better way of doing this. 答:必须有更好的方法来做到这一点。
  • B: what happens if you get a 2 or three digit number instead, eg, "34,2,3,65,125" . B:如果你得到一个2位或3位数字会发生什么,例如"34,2,3,65,125" these will be stored as separate elements of the array when i need it to be one element. 当我需要它作为一个元素时,这些将作为数组的单独元素存储。
  • C: what happens if the number is a negative one, and what if that negative number is 2 or three digits long? C:如果数字是负数,会发生什么,如果负数是2或3位数,该怎么办? Eg, "-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 考虑使用

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.

相关问题 从json字符串中提取对象数组,并将其放入ArrayList中? - Extract an array of objects from json string and put them in ArrayList? 从字符串数组索引中获取数字,并将它们相乘? - Take numbers from string array index, multiply them together? 如何从可充气的Edit Text中读取值并将其放入ArrayList中 - How to read values from inflatable Edit Text and put them in an ArrayList 如何从文件中读取数字并在 java 中对它们执行操作? - How to read numbers from file and take actions on them in java? 如何只取小写字母和数字并将它们放在单独的 TextField 中? - How to take only lower-case letters and numbers and put them in separate TextFields? 如何拆分字符串并将其放在ArrayList上? - How to split String and put it on ArrayList? 从文件中获取数字并对其进行排序 - Take numbers from a file and sort them 如何在Java中的arraylist中按升序排列数字? - How to put numbers in ascending order in an arraylist in Java? 如何从 String 中过滤掉 Unique 元素并将它们存储到 ArrayList - How filter out Unique elements from a String and store them into an ArrayList 如果 Arraylist<string> 还包含数字,我该如何过滤掉它们?</string> - If an Arraylist<String> also contains numbers, how can I filter them out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM