2

So my issue is that I would like to add elements into an ArrayList using the Scanner kb. I also want to make sure that the input (number) is between 0 and 100 inclusive. 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();
}
4
  • 1
    where does myAList come from? It could already have had those numbers added in. Commented Jan 21, 2017 at 4:19
  • 4
    You're going to need to hard code the numbers that you type in. We can't really know what's going wrong when you use nextDouble. You want this to be a minimal reproducible example, with the current missing piece being "complete". Commented Jan 21, 2017 at 4:19
  • Welcome to Stack Overflow. I suggest that you learn some debugging skills. These are very important as a programmer. You can either add System.out.println() statements to your code or use a debugger from an IDE. If you still need help, please post a complete example that we can compile and run ourselves. Also show a sample run to show what input you use and the output you get. Commented Jan 21, 2017 at 4:33
  • Sorry guys, I re edited my post. My concern is that I want to add elements into an ArrayList using the method above but the output does not do me justice. How can I resolve it ? Commented Jan 21, 2017 at 4:48

2 Answers 2

2
  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(); are not required in the code. nextDouble() takes care of accepting a return and moving to the next line on console.

  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. Simply after the while loop, do a

    myAList.add(number);
    
  4. Be careful, if myAList would be null, your statement myAList.trimToSize(); can throw NPE. Since it is out of the if block where you do a null-check. Move it inside the if would be what I would suggest.


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()));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yea that was a syntax error. But the problem is I think I did the code wrong. I want it to prompt a user to enter a number between 0 and 100 inclusive, and to go into the ArrayLists. After reviewing the code minus that syntax error, does it seem correct as to what I want it to do ?
Thanks, but when I print it out. It prints each element twice. I cannot see why it does that ?
edited the answer. Cannot see how you are printing it.
1

if you are using final keyword then make sure you are not choosing dead end. 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

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);
        }
    }

3 Comments

the condition in the question is correct, if anyone of them is null, the if doesn't execute. It can also be read as if (myAList != null && kb != null))
@nullpointer try passing any null either ArrayList or Scanner
ya please do test it at your end as well and let me know what the concern then is. if any of them is null it shouldn't execute the if block and that is what is intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.