简体   繁体   English

如何使用扫描仪将元素添加到ArrayList中

[英]How can I add elements into ArrayList using Scanner

So my issue is that I would like to add elements into an ArrayList using the Scanner kb. 所以我的问题是我想使用Scanner kb将元素添加到ArrayList中。 I also want to make sure that the input (number) is between 0 and 100 inclusive. 我还想确保输入(数字)在0到100之间(包括0和100)。 The code I have does not run as I want. 我拥有的代码无法按我的意愿运行。 How can I resolve this ? 我该如何解决? Thanks. 谢谢。

public static void addNum(final ArrayList<Double> myAList, final Scanner kb)
{
    //Prompt user for a number 0-100
    if(!(kb == null || myAList == null))
    {
        System.out.println("Please enter a number between (0 - 100): ");
        double number = kb.nextDouble();
        kb.nextLine();
        while(number < 0 || number > 100)
        {
            System.out.println("That was not a qualified number, try again");
            number = kb.nextDouble();
            kb.nextLine();
        }

        for(int x = 0; x < myAList.size() - 1; x++);
        {
            myAList.add(number);
        }
    }   
    myAList.trimToSize();
}
  1. Change your statement 更改您的声明

     for(int x = 0; x < myAList.size() - 1; x++); // the semicolon terminates loop without any action block // myAList.size() would keep on increaing with every `add()` inside the loop. 

    to

     int listSize = myAList.size(); for(int x = 0; x < listSize - 1; x++) { myAList.add(number); } 
  2. The statements kb.nextLine(); 语句kb.nextLine(); are not required in the code. 代码中不需要。 nextDouble() takes care of accepting a return and moving to the next line on console. nextDouble()负责接受返回值并移至控制台的下一行。

  3. Contradictory to (1), If you just want to add the number inputted to the existing list, you don't require the for loop anyway. 与(1)相反,如果只想将输入的数字添加到现有列表中,则无论如何都不需要for循环。 Simply after the while loop, do a while循环之后,只需执行

     myAList.add(number); 
  4. Be careful, if myAList would be null , your statement myAList.trimToSize(); 注意,如果myAListnull ,则您的语句myAList.trimToSize(); can throw NPE. 可以扔NPE。 Since it is out of the if block where you do a null-check. 由于它不在if块中,因此您不能进行空检查。 Move it inside the if would be what I would suggest. if我建议的if将其移入内部。


This should just work fine - 这应该很好-

public static void addNum(final ArrayList<Double> myAList, final Scanner kb) {
    if (!(kb == null || myAList == null)) {
        System.out.println("Please enter a number between (0 - 100): ");
        double number = kb.nextDouble();
        while (number < 0d || number > 100d) {
            System.out.println("That was not a qualified number, try again");
            number = kb.nextDouble();
        }
        myAList.add(number);
        myAList.trimToSize();
        System.out.println(Arrays.asList(myAList.toArray()));
    }
}

if you are using final keyword then make sure you are not choosing dead end. 如果您使用的是final关键字,请确保您没有选择“死胡同”。 There is minor bug in your code if(!(kb == null || myAList == null)) can fail if any one of them is null so make sure you check properly and below is working example if(!(kb == null || myAList == null))您的代码中if(!(kb == null || myAList == null))任何一个为nullif(!(kb == null || myAList == null))可能会失败,因此代码中存在一些小错误,因此请确保您检查正确并且下面的示例有效

public void addNum(final List<Double> myAList, final Scanner kb) {
        double number;
        if (!(myAList == null && kb == null)) {
            System.out.println("Please enter a number between (0 - 100): ");
            number = kb.nextDouble();
            while (number < 0d || number > 100d) {
                System.out.println("please enter double and number must be betwen  0 to 100");
                number = kb.nextDouble();
            }
            myAList.add(number);
            System.out.println(myAList);
        }
    }

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

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