简体   繁体   English

使用分隔符将字符串数组转换为int

[英]Converting string array to int with a delimiter

I can't figure out why I'm getting a null pointer exception. 我无法弄清楚为什么我得到一个空指针异常。 I'm trying to convert a line numbers that the user enters after a prompt, and I want to deliminate it either a space " " a comma "," or a comma and a space ", ". 我正在尝试转换用户在提示后输入的行号,并且我想要将其标记为空格“”逗号“,”或逗号和空格“,”。

Here's my code, I'm getting a null pointer exception on the nums[i]=Integer.parseInt(holder[i]); 这是我的代码,我在nums [i] = Integer.parseInt(holder [i])上得到一个空指针异常; line. 线。 I just can't figure out why. 我只是想不通为什么。

String again="n";
    int[] nums = null;

    do {
        Scanner scan = new Scanner (System.in);

        System.out.println("Enter a sequence of integers separated by a combination of commas or spaces: ");
        String in=scan.nextLine();
        String[] holder=in.split(",| |, ");

            for (int i=0; i<holder.length; i++) {

                nums[i]=Integer.parseInt(holder[i]);
                System.out.print(nums[i]);
            }


    }
    while (again=="y");

Ok Thanks everyone, I got it working by initializing the length of the nums array to the length of the holder array as suggested in the accepted answer. 好的感谢大家,我通过将nums数组的长度初始化为持有者数组的长度来实现它,如接受的答案中所建议的那样。 Like this: 像这样:

int[] nums = new int[holder.length];

I have a second question though, as my regex seems to be failing, I can get it to read if delineated by "," or by " " but not by ", " any ideas? 我有第二个问题,因为我的正则表达式似乎失败了,如果用“,”或“”而不是“,”任何想法描述,我可以读它吗?

Here's my error: 这是我的错误:

Enter a sequence of integers separated by a combination of commas or spaces: 
    1, 2, 3
    Exception in thread "main" 1java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at SortComparison.main(SortComparison.java:20)

Your null pointer exception is caused by the fact that you have initialized the nums array to null, then try to "point" to it in your for loop. 您的空指针异常是由于您已将nums数组初始化为null,然后尝试在for循环中“指向”它而引起的。 You can lose int[] nums = null and add: 您可以丢失int[] nums = null并添加:

int[] nums = new int[holder.length];

immediately before the for loop (after you've created the holder array, obviously). 紧接在for循环之前(显然你创建了holder数组之后)。

You have set 你已经设定好了

int[] nums = null;

and then try to access 然后尝试访问

num[i]

which gives you the NullPointerException. 这会给你NullPointerException。 You first need to contruct the array to hold the required number of elements: 首先需要构造数组以保存所需数量的元素:

int[] nums = new int[holder.length] 

You better print your holder[i] before parsing it into an Integer, to see what's in it. 你最好打印你的holder[i]然后再将其解析为整数,看看里面有什么。 I am guessing that holder[i] is not having a valid value for an Integer. 我猜测holder[i]没有Integer的有效值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM