Error with do while loops...HELP!

0 freya buison · October 16, 2015

public class LoopAverages 
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
double num, average, total = 0;
int numbers, choice;

System.out.println("Welcome to the Average Calculator.");
System.out.println("Please enter the number  of numbers you want to find the average of.");
numbers = reader.nextInt();


do
{
System.out.println("Please enter " + numbers + " numbers.");
num = reader.nextDouble();

total = total + num;
average = total/numbers;

System.out.println("Press 0 to stop entering numbers, or any other number to continue");
choice = reader.nextInt();

}while(choice != 0);
}
}

I need to code an average calculator where the user can enter as many numbers as they want and find the average, but when i run it, it wont let me enter all of the numbers! Also, i needed to put in the option to press 0 to break the program but it just comes after  the numbers and prevents me from entering them in... HELP ME!!!

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Whitney · October 16, 2015
There are a number of problems with your code.
Scanner is a tool to get user input from the System i.e. Console, as used here.

1. You have to understand that the Scanner next methods block the program from progressing until the user presses enter.
2. Users don't always follow instructions.
3. next int() will throw an exception and crash your program if the user doesn't enter an integer..
4. nextDouble() will interpret and integer as a double but will crash if say the user enter four.
5. I ran your program and it never tell the user the average.
6. If I enter 5 at your press 0 prompt the program continues the while loop so I am not sure why you say it prevents you from entering them.
7. nothing prevents the user from entering 0 for the number of numbers, and this will surely crash the program

Belois an image of the Eclipse screen when I ran your program in debug mode
If you scroll to the right you will even see the watch variable values. (there is a little scroll line that appears above the image when you click it)
Here my version of your homework. As a rule I don't  do homework but I got carried away.
import java.util.Scanner;
public class LoopAverages {
    public static void main(String[] args) {
     Scanner reader = new Scanner(System.in);
     double num, average = 0, total = 0;
     int numbers = 0, choice =1;
     String strNextNum = "\nPlease enter a number, or  enter 0 to quit.";
    
     System.out.println("Welcome to the Average Calculator.");
 
    
     do {
     System.out.println(strNextNum);
     num = reader.nextDouble();
     if (num>0){
         numbers++;
         total = total + num;
         average = total/numbers;
     }else {
         choice = 0;
     }

    
     }while(choice != 0);
     reader.close(); // should add this
     System.out.println("Numbers entered = " + numbers);
     System.out.println("Total = " + total);
     System.out.println("Average = " + average);
    }
}


0 Gary Whitney · October 16, 2015
I don't know where the image went in my previous post.
I meant to say
Below is an image of the Eclipse screen when I ran your program in debug mode.
Note: a little horizontal scroll bar will appear above the image when you click it.


/images/forum/upload/2015-10-16/06cc29515217bd26afb5527af1e12b1b.png
  • 1

Java / Android Development

107,161 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator