programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# List of numbers input from scanner into an arraylist

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Hi, I'm new to programming and I'm studying for my test. I've come across this type of issue before and I've been able to get around it but I want to know how to actually fix it!
After the user inputs their list of numbers (separated by a whitespace) nothing happens... because while is in an infinite loop but I don't know how to break it (btw: we haven't learned "break" yet).

lowercase baba
Posts: 13018
66
• Number of slices to send:
Optional 'thank-you' note:
Don't assume things. Why do you think you have an infinite loop? try putting in "1 2 3 a" as your input and see what you get...

Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:

Roxy Hasior wrote:because while is in an infinite loop

What infinite loop?

but I don't know how to break it

Apparently you do, since you have code to do just that.

So I guess I'm not sure what your question is.

Rancher
Posts: 4062
56
• Number of slices to send:
Optional 'thank-you' note:
I'm guessing the "infinite loop" is the program waiting for input, blocking on nextDouble().

fred rosenberger wrote:try putting in "1 2 3 a" as your input and see what you get...

And be sure to hit enter - console input is line-buffered, meaning your program won't see any of it until you end a line with "enter".

fred rosenberger
lowercase baba
Posts: 13018
66
• Number of slices to send:
Optional 'thank-you' note:
My guess is that the user is doing something like this:

C:\slop>java Average
Please enter a list of numbers:
1 2 3 4

at which point the Scanner is still waiting for input, so it looks like it's in an infinite loop.

after you do your "list of numbers (separated by a whitespace) " and (i assume) hit enter...you can continue entering numbers:
C:\slop>java Average
Please enter a list of numbers:
1 2 3 4
74
92
-123

you can keep entering numbers until you enter a NON-number...like "done":

C:\slop>java Average
Please enter a list of numbers:
1 2 3 4
74
92
-123
done
The average is: 7.571428571428571

C:\slop>

Mike Simmons
Rancher
Posts: 4062
56
• Number of slices to send:
Optional 'thank-you' note:
Mmm, yes, that does seem more likely.

Roxy Hasior
Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Thanks for everyone's input. This was my first time posting on a programming forum.
So, it looks like in order to alert the scanner that the user is done inputting there must be some kind of signal (a non integer input).

boolean done = false;
while (!done)
{
...
}

I guess I was trying to avoid making the user input anything other than their numbers because this was the original question:

Write a program that prompts the user to a list of numbers, all on one line. If the list is empty, the program prints “No average”. Otherwise, the program then computes and prints the average of those numbers.

Sample run, with user input underlined:

Enter numbers: 10.5 7 -2.4
Average: 5.033333333333333

Write only one program class named Average with only a main method. Import any required class(es).

Mike Simmons
Rancher
Posts: 4062
56
• Number of slices to send:
Optional 'thank-you' note:
Perhaps you can split the problem of gathing up input into two parts:

1. Get a single line of user input. Is there a method on Scanner that you can use for this?

2. Take that line and split it up into a set of different numbers extracted from that line. There are several ways to do this, but I would suggest creating a new Scanner, that takes the line (a String) as input.

Does that help?

Roxy Hasior
Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Yay! It works thanks. This is what I did:

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?