Kaushank
Kaushank

Reputation: 13

Finding maximum number from a loop

I am trying to find maximum number from a loop and it's number of occurrences. Here is the code I have written so far.

public class MaxNumbers {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int max = 0;
        int number = 0;
        int count_max = 0;
        while (true) {
             number = input.nextInt();
            if (number == 0) {
                break;
            }
            number = max;
            if (max > number) {
                max=number;
                count_max++;
            }

        }
        System.out.println("Max Number: " + number);
        System.out.println("Occurences: " + count_max);
    } 

I getting only zero in the output, any logical error I am making?

Upvotes: 0

Views: 12416

Answers (6)

user4826372
user4826372

Reputation: 1

Here's a corrected version of the code with the mistakes you made highlighted.

Scanner input = new Scanner(System.in);
int max = 0;
int number = 0;
int count_max = 0;
while (true) {
    number = input.nextInt();
    if (number == 0) {
        break;
    }
    // you are assigning number = max, and max is 0 initially, so number
    // =0;
    // number =max;
    // if number =0 and max =0 then below condition will never          // satisfied.
    if (max < number) {
        max = number;
        // it means every time when you have any number greater then the                // second one just add it.
            // count_max++;
            count_max = 0;
        }

    // This condition will make sure that only add count by 1 if max =
    // currentNum
    if (max == number) {

        count_max++;
    }

}
// You have written number instead of max down there.
System.out.println("Max Number: " + max);
System.out.println("Occurences: " + count_max);

Upvotes: 0

T I
T I

Reputation: 9933

This is my version of what I think you are trying to achieve, it is mostly working, please note that you have to start typing in numbers if you start with a string it fails as you describe, also note numbers larger then long get ignored or 'trimmed'

import java.util.Scanner;

public class Test {

    public static final void main(String[] args) {
        long a = 0, b = 0, c = 0, d = 0;

        System.out.println("Type some numbers!");

        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();
        Scanner ln = new Scanner(in);

        while (ln.hasNextLong()) {
            a = ln.nextLong();
            if (a > b) {
                b = a;
                ++d;
            }
            ++c;
        }

        System.out.println("\n   info:");
        System.out.println("     highest:" + b);
        System.out.println("     iterations:" + c);
        System.out.println("     overrides:" + d);
    }
} 

Upvotes: 2

Erica
Erica

Reputation: 2251

I think that the line you have with "number = max" needs to be modified. The code you have there is setting the value of number to the value of max every time through the loop. Since this has the value of 0 the first time through the loop, it will just keep on being zero indefinitely. That means that your max never changes.

So you don't want that line, but you do want something like it. If the number has the same value as (==) max, then you need to add 1 to count_max.

The last If block needs a slight tweak too. If you find a new highest value (ie, number > max, not the other way around), then you need to reset your count_max to 1, not add 1 to it.

Upvotes: 0

John Ericksen
John Ericksen

Reputation: 11113

Couple of problems with the code. This should work for you:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int max = Integer.MIN_VALUE;
        int number = 0;
        int count_max = 0;
        while (true) {
            number = input.nextInt();
            if (number == 0) {
                break;
            }
            if(max == number){
                count_max++;
            }
            if (max < number) {
                max=number;
                count_max = 1;
            }
        }
        System.out.println("Max Number: " + max);
        System.out.println("Occurences: " + count_max);
    }

Upvotes: 0

Martin Booth
Martin Booth

Reputation: 8595

Yes there is, you have the line:

number = max

followed by

if (max > number) {

max will never be greater than number since you just set them to be equal in the preceding line

Upvotes: 1

Steve Wang
Steve Wang

Reputation: 1824

remove the line number = max

Upvotes: 0

Related Questions