evanparial
evanparial

Reputation: 45

Trying to find the minimum number within Scanner user input in Java

I'm coding a test averager in Java. I have to use do while, while, if loops.

I have the user inputting test scores, asking for a negative number to leave the while loop.

I have to display the number of scores entered, the highest, the lowest, and the average.

However, I am having problems displaying the lowest number as it as popping up as 0.

For input of 95, 93, 92, 91, and -1:

The number of scores entered: 4 The Highest: 95 The Lowest: 0 The average is: 92

My code:

import java.util.*;

public class Lab7 
{

    public static void main(String[] args) 
    {
        System.out.println("This program computes the average of");
        System.out.println("a list of (nonnegative) exam scores.");
        int sum;
        int numberOfTests;
        int testInput;
        String answer;
        int minimum = 0;
        int maximum = 0;
        Scanner keyboard = new Scanner (System.in);
        
        do
        {
            System.out.println();
            System.out.println("Enter all the scores to be averaged.");
            System.out.println("Enter a negative number after");
            System.out.println("you have entered all the scores.");
            sum = 0;
            numberOfTests = 0;
            testInput = keyboard.nextInt();

            while (testInput >= 0)
            {
                sum = sum + testInput; 
                numberOfTests++;
                

                 if (testInput < minimum)
                {
                    minimum = testInput;
                }
                
                if (testInput > maximum) 
                {
                    maximum = testInput;
                }
    
                testInput = keyboard.nextInt();
            }

            if (numberOfTests > 0) 
            {
                System.out.println("The number of scores entered: " + numberOfTests);
                System.out.println("The Highest: " + maximum);
                System.out.println("The Lowest: " + minimum);
                System.out.println("The average is: " +
                (sum / numberOfTests));    

            }

            else 
            {
                System.out.println("No scores to average.");
            }

            System.out.println("Want to average another exam?");
            System.out.println("Enter yes or no.");
            answer = keyboard.next();
        }
        
        while (answer.equalsIgnoreCase("yes"));
    }

    
}

Upvotes: 2

Views: 933

Answers (2)

Sumit Singh
Sumit Singh

Reputation: 1111

This is due to this line int minimum = 0; as your input scores are greater than 0 thus your this condition will never be satisfied if (testInput < minimum) and the value of minimum won't never-ever changes and will remain as 0. Hence define it as minimum = Integer.MAX_VALUE

Following is your code in a working state and I also made some optimizations which will help you in a better way of writing code:

public class Solution{
    public static void main(String[] args) 
    {
        int sum = 0, numberOfTests = 0, testInput , minimum = Integer.MAX_VALUE , maximum = Integer.MIN_VALUE;
       
        System.out.println("This program computes the average of \na list of (nonnegative) exam scores. \n");
        
        Scanner keyboard = new Scanner (System.in);
        
        do
        {
            System.out.println();
            System.out.println("Enter all the scores to be averaged.");
            System.out.println("Enter a negative number after \nYou have entered all the scores.");
            
            testInput = keyboard.nextInt();

            while (testInput >= 0){
                sum += testInput; 
            
                numberOfTests++;                

                if (testInput < minimum) minimum = testInput;
                
                if (testInput > maximum) maximum = testInput;
                    
                testInput = keyboard.nextInt();
            }

            if (numberOfTests > 0) 
            {
                System.out.println("The number of scores entered: " + numberOfTests);
                System.out.println("The Highest: " + maximum);
                System.out.println("The Lowest: " + minimum);
                System.out.println("The average is: " +
                ((float)sum / numberOfTests));    
            }

            else 
                System.out.println("No scores to average.");
            

            System.out.println("Want to average another exam?");
            System.out.println("Enter yes or no.");

        }
        
        while (keyboard.nextLine().trim().equalsIgnoreCase("yes"));
        
        keyboard.close();
    }
    
}

Thanks

Upvotes: 0

QBrute
QBrute

Reputation: 4536

You initialise minimum with 0, so for positive inputs it will always be the smallest value. That means if (testInput < minimum) will always be false. To fix this, initialise it with Integer.MAX_VALUE instead.

In general, it is a good idea to start a maximum value with the lowest possible value, like Integer.MIN_VALUE or Double.MIN_VALUE or in your case with 0, since there are no negative scores. Furthermore, the minimum should be initialised with the largest possible value, like described above.

That way, your checks will always work as intended.

Upvotes: 5

Related Questions