-1

As the title stated, I am trying to find the maximum and minimum values, the amount of evens and odds numbers, and the average of all inputted numbers.

The problem. As I run my code, my odds and evens counter seem to read their opposite, odd would read an even input and even would read an odd input. As for my average, I have no clue what is wrong with it, all I know is that it would only find the average of a proper fraction.

Also, as I input quit, I get for my largest number 214XXXXXXXX

Example of my output will be pasted at the end.

System.out.println("Enter a sequence of integers. Any non-integer to quit");
Scanner scan = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = 0;
int count = 0;
int sum = 0;
int oddsCounter = 0;
int evensCounter = 0;
int getInt;
double average = 0;

while (scan.hasNextInt()) {

    getInt = scan.nextInt();
    if (getInt % 2 == 0) {
        evensCounter++;
        System.out.println("even: " + evensCounter);
    } else {
        oddsCounter++;
        System.out.println("odd: " + oddsCounter);
    }

    if (getInt < min) {
        min = getInt;
    } else if (getInt > max) {
        max = getInt;
    }

    sum += getInt;
    System.out.println("sum " + sum);
    count++;
    System.out.println("count " + count);
    average = (double) sum / (count);
    System.out.println("average " + average);
}

System.out.println("smallest: " + min);
System.out.println("largest: " + max);
System.out.println("even: " + oddsCounter);
System.out.println("odd: " + evensCounter);
System.out.println("average: " + average);

Result:

Enter a sequence of integers. Any non-integer to quit
9 //input 1
odd: 1
sum 9
count 1
average 9.0
3 //input 2
odd: 2
sum 12
count 2
average 6.0
7 //input 3
odd: 3
sum 19
count 3
average 6.333333333333333
1 //input 4
odd: 4
sum 20
count 4
average 5.0
q //input 5: QUIT
smallest: 1
largest: 7 //This should be 9
even: 0
odd: 4
average: 5.0

Result 2:

Enter a sequence of integers. Any non-integer to quit
q //quit
smallest: 2147483647 //This should be 0
largest: 0
even: 0
odd: 0
average: 0.0

Any help would be appreciated. Thank you!

Mat
  • 202,337
  • 40
  • 393
  • 406
user2197917
  • 43
  • 1
  • 10

4 Answers4

0

First

9 wasn't assigned to max. Since 9 < min, The code evaluates min = getInt; and skip the else if part.

It should be :

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

if (getInt > max) 
{
     max = getInt;
}

Because a number can be both min and max.

Second

smallest: 2147483647 //This should be 0

This happen because you type in a character but scan in as an integer by usinggetInt = scan.nextInt(); (as well as store the input as an int).

You can fix this scan in the input as String first. Then, you determine if the input is an int or not by creating a method to check it. :

public static boolean isInteger(String s) 
{
    try 
    { 
        Integer.parseInt(s); 
    } 
    catch(NumberFormatException e) 
    { 
        return false; 
    }
    // only got here if we didn't return false
    return true;
}

Then, check if the input is an integer before you do the calculation.

Community
  • 1
  • 1
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
0

Take out else before checking if getInt is greater than max. You need to check for both the conditions, so don't put else there.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
0

As to your other problem, you have to initialize min as 0. After that, check if the input is your first, and if so set min as that.

boolean isFirst = true;
while (scan.hasNextInt()) {

getInt = scan.nextInt();
if(isFirst)
{
     min = getInt;
     isFirst=false;
}
//Rest of your code

}

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
0
if (getInt < min) {
        min = getInt;
    } else if (getInt > max) {
        max = getInt;
    }

Change above code as mentioned below . You have to check both condition so change else if to if.

In if - else if

once a condition is satisfied, the appropriate statements are executed and the remaining conditions are not evaluated.

if (getInt < min) {
        min = getInt;
    } 
if (getInt > max) {
        max = getInt;
    }
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • if I put in the sequence -3, -2, -1 the output would be smallest: -3, largest: 0. – user2197917 Mar 23 '13 at 10:19
  • @user2197917 You have to check two conditions so use if() if() not if() else if(). if() else if() will only execute one appropriate statement. try using if() if () once. – Achintya Jha Mar 23 '13 at 10:22