This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

List of numbers input from scanner into an arraylist  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!


 
lowercase baba
Bartender
Posts: 12628
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 3104
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12628
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3104
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmm, yes, that does seem more likely.
 
Roxy Hasior
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3104
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yay! It works thanks. This is what I did:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!