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!
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;
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
Write only one program class named Average with only a main method. Import any required class(es).
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.