如何将空格分隔的整数字符串转换为JAVA中的数组 [英] how to convert an integer string separated by space into an array in JAVA

查看:99
本文介绍了如何将空格分隔的整数字符串转换为JAVA中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串"1 23 40 187 298".该字符串仅包含整数和空格.如何将字符串转换为整数数组,即[1,23,40,187,298]. 这就是我尝试过的方式

public static void main(String[] args) {
    String numbers = "12 1 890 65";
    String temp = new String();
    int[] ary = new int[4];
    int j=0;
    for (int i=0;i<numbers.length();i++)
    {

        if (numbers.charAt(i)!=' ')
            temp+=numbers.charAt(i);
        if (numbers.charAt(i)==' '){
            ary[j]=Integer.parseInt(temp);
            j++;
        }
    }
}

但是它不起作用,请提供一些帮助.谢谢!

解决方案

您忘记了

  • temp重置为空字符串,在解析它以创建新数字的地方
  • 在字符串的末尾将没有空格,所以

    if (numbers.charAt(i) == ' ') {
        ary[j] = Integer.parseInt(temp);
        j++;
    }
    

    将不会被调用,这意味着您需要调用

    ary[j] = Integer.parseInt(temp);
    

    循环后再一次


但更简单的方法是使用split(" ")创建令牌的临时数组,然后像

ary[j] = Integer.parseInt(temp);

那样将每个令牌解析为int

String numbers = "12 1 890 65";
String[] tokens = numbers.split(" ");
int[] ary = new int[tokens.length];

int i = 0;
for (String token : tokens){
    ary[i++] = Integer.parseInt(token); 
}

也可以通过在 Java 8 中添加的流来缩短它:

String numbers = "12 1 890 65";
int[] array = Stream.of(numbers.split(" "))
                    .mapToInt(token -> Integer.parseInt(token))
                    .toArray();


其他方法可能是使用Scanner及其nextInt()方法从输入中返回所有整数.假设您已经知道所需数组的大小,则可以简单地使用

String numbers = "12 1 890 65";
int[] ary = new int[4];

int i = 0;
Scanner sc = new Scanner(numbers);
while(sc.hasNextInt()){
    ary[i++] = sc.nextInt();
}

Suppose I have a string "1 23 40 187 298". This string only contains integers and spaces. How can I convert this string to an integer array, which is [1,23,40,187,298]. this is how I tried

public static void main(String[] args) {
    String numbers = "12 1 890 65";
    String temp = new String();
    int[] ary = new int[4];
    int j=0;
    for (int i=0;i<numbers.length();i++)
    {

        if (numbers.charAt(i)!=' ')
            temp+=numbers.charAt(i);
        if (numbers.charAt(i)==' '){
            ary[j]=Integer.parseInt(temp);
            j++;
        }
    }
}

but it doesn't work, please offer some help. Thank you!

解决方案

You are forgetting about

  • resetting temp to empty string after you parse it to create place for new digits
  • that at the end of your string will be no space, so

    if (numbers.charAt(i) == ' ') {
        ary[j] = Integer.parseInt(temp);
        j++;
    }
    

    will not be invoked, which means you need invoke

    ary[j] = Integer.parseInt(temp);
    

    once again after your loop


But simpler way would be just using split(" ") to create temporary array of tokens and then parse each token to int like

String numbers = "12 1 890 65";
String[] tokens = numbers.split(" ");
int[] ary = new int[tokens.length];

int i = 0;
for (String token : tokens){
    ary[i++] = Integer.parseInt(token); 
}

which can also be shortened with streams added in Java 8:

String numbers = "12 1 890 65";
int[] array = Stream.of(numbers.split(" "))
                    .mapToInt(token -> Integer.parseInt(token))
                    .toArray();


Other approach could be using Scanner and its nextInt() method to return all integers from your input. With assumption that you already know the size of needed array you can simply use

String numbers = "12 1 890 65";
int[] ary = new int[4];

int i = 0;
Scanner sc = new Scanner(numbers);
while(sc.hasNextInt()){
    ary[i++] = sc.nextInt();
}

这篇关于如何将空格分隔的整数字符串转换为JAVA中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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