Andrew De Forest
Andrew De Forest

Reputation: 7408

Finding the smallest integer based on input

I have an assignment to find the minimum, maximum, and average of numbers that a user inputs. Basically, they type in positive integers, seperated by a space and Java scrolls through them and adds them up. I'm able to find the sum, the average, and the largest integers, however, I am unable to find the smallest. I thought the best way to figure this out would be to set the variable representing the smallest int equal to the variable representing the largest int outside of the loop. Then, within the loop, do something like this:

        if(getInt < min)
        {
            min = getInt;
        }

Where getInt is the user-inputted value and min is the minimum integer value. Every time I run this, though, min returns as 0.

Here is my full code:

import java.util.Scanner;

public class exc5
{
    public static void main (String[] args)
    {  
        System.out.println("Write a list of nonnegative integers, each seperated by a space. To signal the end of the list, write a negative integer. The negative integer will not be counted");
        Scanner keyboard = new Scanner (System.in);
        keyboard.useDelimiter(" |\n");

        int count = 0;
        int sum = 0;
        int max = 0;
        int min = max;
        double average = 0;

        boolean notNull = true;

        while(notNull == true)
        {
            int getInt = keyboard.nextInt();

            if(getInt < 0)
            {
                notNull = false;
            }  
            else
            {          
                if(getInt > max)
                {
                    max = getInt;
                }

                if(getInt < min)
                {
                    min = getInt;
                }

                sum += getInt;
                count++;
                average = (sum)/(count);
            }
        }

        System.out.println("Sum = " + sum);
        System.out.println("Average = " + average);
        System.out.println("Max = " + max);
        System.out.println("Minimum = " + min);
    }

}

Any help is appreciated!

Upvotes: 3

Views: 24202

Answers (8)

Bohemian
Bohemian

Reputation: 425448

You need to start with a large min, not 0 (only negative numbers are less then 0). Try this:

int min = Integer.MAX_VALUE;

This is a fairly standard pattern. Also, to be "correct", your code should also include:

int max = Integer.MIN_VALUE;

Then the full range of integer input, positive and negative, is handled.

Edit:

Another, more "classic" approach would be to use an object reference, rather than a primitive (which always has a value), so the initial state can be easily distinguished as null. In this case, using Integer, rather than int for the values would allow testing for null. If I were a university professor marking such work, I would rather see the object approach - using min/max values for primitives will work, but it's a bit of a "trick".

The code wold look like this:

Integer min;

while (...) {

    if (min == null || getInt < min) {
        min = getInt;
    }
}

And similar for max.

Upvotes: 7

Juvanis
Juvanis

Reputation: 25950

Fully working and "cleaned" version of your program:

import java.util.Scanner;

public class exc5
{
    public static void main ( String [] args )
    {
        Scanner keyboard = new Scanner( System.in );
        int count = 0;
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        double average = 0;
        boolean notNull = true;

        while ( notNull == true )
        {
            int getInt = keyboard.nextInt();

            if ( getInt < 0 )
                notNull = false;
            else
            {
                if ( getInt > max )
                    max = getInt;

                if ( getInt <= min )
                    min = getInt;

                sum += getInt;
                count++;
            }
        }

        average = ( sum ) / ( count );

        System.out.println( "Sum = " + sum );
        System.out.println( "Average = " + average );
        System.out.println( "Max = " + max );
        System.out.println( "Minimum = " + min );
    }

}

Upvotes: 1

Richard
Richard

Reputation: 71

you have min set as 0 to start with so getInt will never be less. You should set min at the beginning of the loop and that will solve your issue.

Upvotes: 1

icyrock.com
icyrock.com

Reputation: 28628

You initialize min with 0. Try to initialize with Integer.MAX_VALUE, i.e. it would be best to do:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

Otherwise, your check:

if(getInt < 0)

will break out of the loop and you will never go over 0.

Upvotes: 1

DNA
DNA

Reputation: 42617

You are setting min to zero, so (since the inputs are positive) none of the inputs are less than min.

The normal way to do this is to set the initial value of max to Integer.MIN_VALUE, and the initial value of min to Integer.MAX_VALUE.

This ensures that any input value will affect at least one of min and max - usually both!

Upvotes: 1

m0skit0
m0skit0

Reputation: 25874

That's because you're assigning min = 0, so obviously unless you input a number lower than 0, the minimum is 0. You should initialize min to Integer.MAX_VALUE

int min = Integer.MAX_VALUE;

( or also:

int min = 0xFFFFFFFF;

Just for hacking around :P. Use the first one! :) )

Upvotes: 1

ruakh
ruakh

Reputation: 183612

This:

        int max = 0;
        int min = max;

initializes min to zero. (After all, max hasn't become the largest integer yet.) So this:

                if(getInt < min)

will never be true. Try changing this:

        int min = max;

to this:

        int min = Integer.MAX_VALUE; // largest possible Java `int`

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727107

You should initially set min to Integer.MAX_VALUE, and max to Integer.MIN_VALUE.

Upvotes: 10

Related Questions