Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串中的整数提取到数组中_Java_Arrays_Regex_String_Extract - Fatal编程技术网

Java 将字符串中的整数提取到数组中

Java 将字符串中的整数提取到数组中,java,arrays,regex,string,extract,Java,Arrays,Regex,String,Extract,我需要从字符串中提取整数到数组中 public static void main(String[] args) { String line = "First number 10, Second number 25, Third number 123"; String numbersLine = line.replaceAll("[^0-9]+", ""); int result = Integer.parseIn

我需要从字符串中提取整数到数组中

public static void main(String[] args) {
    String line = "First number 10, Second number 25, Third number 123";
    String numbersLine = line.replaceAll("[^0-9]+", "");
    int result = Integer.parseInt(numbersLine);

    // What I want to get:
    // array[0] = 10;
    // array[1] = 25;
    // array[2] = 123;
}
我已经得到了整数,但我无法将它们放入数组中

public static void main(String[] args) {
    String line = "First number 10, Second number 25, Third number 123";
    String numbersLine = line.replaceAll("[^0-9]+", "");
    int result = Integer.parseInt(numbersLine);

    // What I want to get:
    // array[0] = 10;
    // array[1] = 25;
    // array[2] = 123;
}

如果您有一个类似“10、20、30”的字符串,您可以使用以下命令:

String numbers = "10, 20, 30";

String[] numArray = nums.split(", ");

ArrayList<Integer> integerList = new ArrayList<>();

for (int i = 0; i < x.length; i++) {
    integerList.add(Integer.parseInt(numArray[i]));
}
stringnumber=“10,20,30”;
字符串[]numArray=nums.split(“,”);
ArrayList integerList=新的ArrayList();
对于(int i=0;i
如果您的数字是“10、20、30”之类的字符串,您可以使用以下命令:

String numbers = "10, 20, 30";

String[] numArray = nums.split(", ");

ArrayList<Integer> integerList = new ArrayList<>();

for (int i = 0; i < x.length; i++) {
    integerList.add(Integer.parseInt(numArray[i]));
}
stringnumber=“10,20,30”;
字符串[]numArray=nums.split(“,”);
ArrayList integerList=新的ArrayList();
对于(int i=0;i
替换为空格,而不是用空字符串替换字符。然后就分手了

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String line = "First number 10, Second number 25, Third number 123 ";
        String numbersLine = line.replaceAll("[^0-9]+", " ");

        String[] strArray = numbersLine.split(" ");

        List<Integer> intArrayList = new ArrayList<>();

        for (String string : strArray) {
            if (!string.equals("")) {
                System.out.println(string);
                intArrayList.add(Integer.parseInt(string));
            }
        }

        // what I want to get:
        // int[0] array = 10;
        // int[1] array = 25;
        // int[2] array = 123;
    }
}
import java.util.ArrayList;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args){
String line=“第一个数字10,第二个数字25,第三个数字123”;
字符串numbersLine=line.replaceAll(“[^0-9]+”,”);
字符串[]strArray=numbersLine.split(“”);
List intArrayList=new ArrayList();
用于(字符串:strArray){
如果(!string.equals(“”){
System.out.println(字符串);
add(Integer.parseInt(string));
}
}
//我想要的是:
//int[0]数组=10;
//int[1]数组=25;
//int[2]数组=123;
}
}

替换为空格,而不是用空字符串替换字符。然后就分手了

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String line = "First number 10, Second number 25, Third number 123 ";
        String numbersLine = line.replaceAll("[^0-9]+", " ");

        String[] strArray = numbersLine.split(" ");

        List<Integer> intArrayList = new ArrayList<>();

        for (String string : strArray) {
            if (!string.equals("")) {
                System.out.println(string);
                intArrayList.add(Integer.parseInt(string));
            }
        }

        // what I want to get:
        // int[0] array = 10;
        // int[1] array = 25;
        // int[2] array = 123;
    }
}
import java.util.ArrayList;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args){
String line=“第一个数字10,第二个数字25,第三个数字123”;
字符串numbersLine=line.replaceAll(“[^0-9]+”,”);
字符串[]strArray=numbersLine.split(“”);
List intArrayList=new ArrayList();
用于(字符串:strArray){
如果(!string.equals(“”){
System.out.println(字符串);
add(Integer.parseInt(string));
}
}
//我想要的是:
//int[0]数组=10;
//int[1]数组=25;
//int[2]数组=123;
}
}

您可以尝试使用流api:

String input = "First number 10, Second number 25, Third number 123";

int[] anArray = Arrays.stream(input.split(",? "))
    .map(s -> {
        try {
            return Integer.valueOf(s);
        } catch (NumberFormatException ignored) {
            return null;
        }
    })
    .filter(Objects::nonNull)
    .mapToInt(x -> x)
    .toArray();


System.out.println(Arrays.toString(anArray));
输出为:

[10,25,123]

regex.replaceAll版本将为:

int[] a = Arrays.stream(input.replaceAll("[^0-9]+", " ").split(" "))
    .filter(x -> !x.equals(""))
    .map(Integer::valueOf)
    .mapToInt(x -> x)
    .toArray();

其中输出相同。

您可以尝试使用流api:

String input = "First number 10, Second number 25, Third number 123";

int[] anArray = Arrays.stream(input.split(",? "))
    .map(s -> {
        try {
            return Integer.valueOf(s);
        } catch (NumberFormatException ignored) {
            return null;
        }
    })
    .filter(Objects::nonNull)
    .mapToInt(x -> x)
    .toArray();


System.out.println(Arrays.toString(anArray));
输出为:

[10,25,123]

regex.replaceAll版本将为:

int[] a = Arrays.stream(input.replaceAll("[^0-9]+", " ").split(" "))
    .filter(x -> !x.equals(""))
    .map(Integer::valueOf)
    .mapToInt(x -> x)
    .toArray();

其中输出相同。

您可以使用正则表达式提取数字:

String s = "First number 10, Second number 25, Third number 123 ";
Matcher matcher = Pattern.compile("\\d+").matcher(s);

List<Integer> numbers = new ArrayList<>();
while (matcher.find()) {
    numbers.add(Integer.valueOf(matcher.group()));
}

注意:此解决方案仅适用于整数,但这也是您的要求。

您可以使用正则表达式提取数字:

String s = "First number 10, Second number 25, Third number 123 ";
Matcher matcher = Pattern.compile("\\d+").matcher(s);

List<Integer> numbers = new ArrayList<>();
while (matcher.find()) {
    numbers.add(Integer.valueOf(matcher.group()));
}
注意:此解决方案仅适用于
整数
,但这也是您的要求。

工作代码:

public static void main(String[] args) 
{
    String line = "First number 10, Second number 25, Third number 123 ";
    String[] strArray= line.split(",");
    int[] integerArray =new int[strArray.length];

    for(int i=0;i<strArray.length;i++)
        integerArray[i]=Integer.parseInt(strArray[i].replaceAll("[^0-9]", ""));
    for(int i=0;i<integerArray.length;i++)
        System.out.println(integerArray[i]);
        //10
        //15 
        //123
}
publicstaticvoidmain(字符串[]args)
{
String line=“第一个数字10,第二个数字25,第三个数字123”;
String[]strArray=line.split(“,”);
int[]integerArray=新的int[strArray.length];
对于(int i=0;i工作代码:

public static void main(String[] args) 
{
    String line = "First number 10, Second number 25, Third number 123 ";
    String[] strArray= line.split(",");
    int[] integerArray =new int[strArray.length];

    for(int i=0;i<strArray.length;i++)
        integerArray[i]=Integer.parseInt(strArray[i].replaceAll("[^0-9]", ""));
    for(int i=0;i<integerArray.length;i++)
        System.out.println(integerArray[i]);
        //10
        //15 
        //123
}
publicstaticvoidmain(字符串[]args)
{
String line=“第一个数字10,第二个数字25,第三个数字123”;
String[]strArray=line.split(“,”);
int[]integerArray=新的int[strArray.length];

对于(int i=0;i您确定将获得逗号分隔的字符串吗?如果是,则使用逗号分隔第一个子字符串,然后执行相同的操作。的可能重复是否确定将获得逗号分隔的字符串?如果是,则使用逗号分隔第一个子字符串,然后执行相同的操作。的可能重复