File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes List of numbers input from scanner into an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List of numbers input from scanner into an arraylist" Watch "List of numbers input from scanner into an arraylist" New topic
Author

List of numbers input from scanner into an arraylist

Roxy Hasior
Greenhorn

Joined: Mar 27, 2012
Posts: 3
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).
Thanks in advance for your help!


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Mmm, yes, that does seem more likely.
Roxy Hasior
Greenhorn

Joined: Mar 27, 2012
Posts: 3
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).

For example adding a
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Mar 27, 2012
Posts: 3
Yay! It works thanks. This is what I did:

 
 
subject: List of numbers input from scanner into an arraylist
 
Similar Threads
How to write an algorithm for an average of values?
A question about Scanner
Input Validation Help
Counting in Array List/ Standard deviation
ArrayList Problem