John Huber
John Huber

Reputation: 33

java code, finding the min, max and average of floating point numbers

I wrote the below code everything works fine except it never executes the last loop so the last value inputted is never calculated into the min/max/average. Any idea where I went wrong?

import java.util.Scanner;
public class Program4_JohnHuber {

public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

   double total = 0;
   //double numbers = 0;
   int count = 1;
   double largest = 0;
   double smallest = Double.MAX_VALUE;
   double average = 0;

   System.out.print ("Please enter the grades, (Press enter when finished): ");
   {
       while (input.hasNextDouble()&& count<5)
       {
           double entered = input.nextDouble();
           total = total + entered;
           //count++;

       //double average = 0;
       if (count > 0)
       {
           average = total/count;
       }
       if (entered > largest)
       {
           largest = entered;
       }
       if (entered < smallest)
       {
           smallest = entered;
       }
       count++;
       }

   }
       System.out.printf ("Your average grade is %3.2f,\n", average);
       System.out.printf ("Your highest grade is %3.2f \n", largest);
       System.out.printf ("Your lowest grade is %3.2f \n", smallest);



}
}

Upvotes: 1

Views: 785

Answers (1)

ajb
ajb

Reputation: 31689

There are two errors in your program (assuming your intent is to input 5 numbers):

  1. You're using count to indicate the number of grades you've entered already. Before you've entered any grades, how many grades have you entered? That's the value count should have, but you've initialized it to the wrong value.
  2. The other issue is in how you've written the while:

       while (input.hasNextDouble() && count<5)
    

Suppose you've fixed the first problem, so that it now lets you enter 5 numbers and keeps statistics on those numbers. Now it goes back up to the while loop and evaluates the boolean expression.

At this point, count is 5, so you want to exit the loop. But that doesn't happen, because input.hasNextDouble() is evaluated first. Since you're using a scanner on System.in, that means that the program waits until either you type in something that isn't blank, or until you indicate the end of input with CTRL+D on Linux or CTRL+Z on Windows. After it finds the next item in the input, it will then exit the loop if it can't find a double (e.g. you type in some letters); or if you put in a double, then it checks count.

The combination of these two errors is why the program appears to be ignoring the last input: (1) it only does the computation on 4 grades, not 5, because of the error in initializing count, and (2) it asks for a 5th grade anyway, because the parts of the while loop condition are in the wrong order.

To fix the second problem, change it to

while (count < 5 && input.hasNextDouble())

This checks count first, and exits the loop immediately when you have enough grades, instead of looking for more input.

Upvotes: 2

Related Questions