Low Scores
Low Scores

Reputation: 135

Beginning Java : Finding the greatest number out of a list

I am trying to write a code that prompts the user for three numbers and the program is supposed to say what number is the greatest. I didn't want to do a bunch of "System.out.print" in the if and else if statements. The error according to the debugger is that "greatest" and "greatest1" have not been initialized.

import java.util.Scanner;

public class number1
{

    public static void main(String[] args)
    {

        double a, b, c;
        double greatest, greatest1;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter one number :");
        a  = keyboard.nextDouble();
        System.out.print("Enter another number :"); 
        b  = keyboard.nextDouble();
        System.out.print("Enter a third number :");
        c  = keyboard.nextDouble();

        if(a > b && a > c) {
            greatest = a;
        } //end of if statement

        else if(b > a && b > c){   
            greatest = b;
        }

        else if(c > a && c > b) { 
            greatest = c;
        }
        else if(a==b && c < a) {
            greatest = a;
            greatest1 = b;

        }

        else if(a==c && b < a) {
            greatest = a;
            greatest1 = c;
        }

        else if(b==c && a < b) {
            greatest = b;
            greatest1 = c;
        }

        else {
            System.out.print("All of the numbers are greatest");
        }
        System.out.print("The greatest number is: " +greatest+ "and" +greatest1);
    }
} 

Upvotes: 3

Views: 1136

Answers (5)

dreamcrash
dreamcrash

Reputation: 51443

Since, the others have already point out the problem. I am going to point out that you can find the biggest number out of three number by just doing :

biggest = Math.max(a, Math.max(b,c));

You can replace all the conditionals in your code.

Imagine that you want the max of a set of integers (e.g., an array of ints). You can do something like:

biggest = array[0]; // initialize max to the first number of the set

iterate over the set and checking the max:

for(int i = 1; i < array.size; i++) 
   biggest = Math.max(biggest,array[i]);

or with Java Streams:

Arrays.stream(array).max().getAsDouble()

You can also make your own max function, for your case can the following:

public double maxDouble (double a, double b){
       if(a > b) return a;
       else return b;
}

You can read here more detailed information about the Math class and their methods (such as public static double max(double a,double b).

Upvotes: 4

Rais Alam
Rais Alam

Reputation: 7016

Take input from keyboard and create a double array and then try below code.

double dblArray[] = {24.0,40.2,38.9};
Arrays.sort(dblArray);

System.out.print("The greatest number is: " +dblArray[dblArray.length-1]+ " and " +dblArray[dblArray.length-2]);

You can print n greatest numbers.

Upvotes: 1

user000001
user000001

Reputation: 33327

Another tip: If you press enter after entering each number, you should read the newline \n after you read each number.

E.g.

a  = keyboard.nextDouble();

Should be changed to

a  = keyboard.nextDouble();
keyboard.nextLine();

etc.

Upvotes: 1

Daniel Kaplan
Daniel Kaplan

Reputation: 67360

To fix this, you need to make sure greatest and greatest1 are always assigned to a value. What would happen if it went into this code block:

if (a > b && a > c) {
    greatest = a;
} //end of if statement

greatest1 would not be assigned so when it printed it out in the last statement that says

System.out.print("The greatest number is: " + greatest + "and" + greatest1);

It's giving you an error.

Upvotes: 3

AlexWien
AlexWien

Reputation: 28727

As a general hint: Initialize with 0:

double greatest = 0;
double greatest1 = 0;

But be warned, in your case this indicates an error in your code logic. Fix that logic, otherwise the result will be 0, which can be wrong.

Upvotes: 0

Related Questions