Tony Mendez
Tony Mendez

Reputation: 1

Why does my code not return the Max value of N?

The output is some huge number. I have tried about 10 different variations of the code. I understand that the biggest issue is I am not comprehending correctly. I am trying to self teach. This problem has me completely stumped. Any help would be appreciated.

package com.codegym.task.task05.task0532;
import static java.lang.Integer.*;
import java.io.*;

/* 
Task about algorithms
Write a program that:
1. reads a number N (must be greater than 0) from the console
2. reads N numbers from the console
3. Displays the maximum of the N entered numbers.


Requirements:
1. The program should read the numbers from the keyboard.
2. The program must display a number on the screen.
3. The class must have a public static void main method.
4. Don't add new methods to the Solution class.
5. The program should display the maximum of the N entered numbers.
6. The program should not display anything if N is less than or equal to 0.
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int maximum = MAX_VALUE;
        int N = Integer.parseInt(reader.readLine());


        int i;
        int N2 = 0 ;
        for (i = 0; i != N; i++){
            N2 = Integer.parseInt(reader.readLine());
            maximum = N2;

        }
        System.out.println(maximum);

    }
}

Upvotes: 0

Views: 205

Answers (3)

k.wahome
k.wahome

Reputation: 1062

The value assigned to maximum is overwritten by every new input value parsed in from the reader at each iteration:

int i;
int N2 = 0 ;
for (i = 0; i != N; i++){
    N2 = Integer.parseInt(reader.readLine());
    maximum = N2; // overwrites maximum with the newly parsed int

}

What you need is a way to keep track of the maximum number on each iteration by comparing with the last known maximum; starting off with the minimum value of integer as the maximum.

import static java.lang.Integer.*;
import java.io.*;

/* 
Task about algorithms
Write a program that:
1. reads a number N (must be greater than 0) from the console
2. reads N numbers from the console
3. Displays the maximum of the N entered numbers.


Requirements:
1. The program should read the numbers from the keyboard.
2. The program must display a number on the screen.
3. The class must have a public static void main method.
4. Don't add new methods to the Solution class.
5. The program should display the maximum of the N entered numbers.
6. The program should not display anything if N is less than or equal to 0.
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        if (N < 1) {
            return; // covers requirement 6. The program should not display anything if N is less than or equal to 0.
        }


        int maximum = MIN_VALUE; // start off with the min value of integer as max value
        int i;
        int N2;
        for (i = 0; i < N; i++){
            N2 = Integer.parseInt(reader.readLine());
            maximum = Math.max(N2, maximum);

        }
        System.out.println(maximum);
    }
}

Upvotes: 0

Vishakha Lall
Vishakha Lall

Reputation: 1324

Why don't you try this?

import static java.lang.Integer.*;
import java.io.*;

/* 
Task about algorithms
Write a program that:
1. reads a number N (must be greater than 0) from the console
2. reads N numbers from the console
3. Displays the maximum of the N entered numbers.


Requirements:
1. The program should read the numbers from the keyboard.
2. The program must display a number on the screen.
3. The class must have a public static void main method.
4. Don't add new methods to the Solution class.
5. The program should display the maximum of the N entered numbers.
6. The program should not display anything if N is less than or equal to 0.
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int maximum = Integer.MIN_VALUE;
        int N = Integer.parseInt(reader.readLine());


        int i;
        int N2 = 0 ;
        for (i = 0; i != N; i++){
            N2 = Integer.parseInt(reader.readLine());
            maximum = Math.max(N2,maximum);

        }
        System.out.println(maximum);
    }
}

The idea here is to set the value of maximum initially as a very small value so that we can compare and get the highest value from the set of values. At each iteration, a new number is read and compared with the existing value of maximum. If it is higher than maximum, it replaces maximum.
Here's what I tried as the input

5
4
8
9
1
2

Output

9

5 is the number of integers we enter, so initially lets assume a very small value for maximum (-2134). When it sees the first value (4), compares it with the current maximum (4>-2134), it assigns the value of maximum as 4. The iteration continues and the maximum value is 9, on comparing with values 1 and 2, the value of maximum remains unchanged. Hope it helps!

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

Your logic is incorrect, and overwrites the maximum at each iteration without actually checking the value. The logic you want is to start off at the MIN_VALUE of Integer, and assign a new maximum at each incoming value, should it be greater than the previous maximum.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int maximum = MIN_VALUE;
int N = Integer.parseInt(reader.readLine());

for (int i=0; i < N; i++){
    int N2 = Integer.parseInt(reader.readLine());
    if (N2 > maximum) {
        maximum = N2;
    }
}
System.out.println(maximum);

Note: There is one subtle but unlikely edge case here. Should the user enter N = 0 for the number of inputs, the maximum reported would be Integer.MIN_VALUE. This is somewhat meaningless, but is an artifact of the logic being used above.

Upvotes: 1

Related Questions