Tony L
Tony L

Reputation: 51

A persistent compilation error when finding the maximum of three integers using java

I'm new to Java and trying to find a solution to a problem that has been returning a persistent compilation error.

I have pasted my code as below:

import java.util.*;

class MaxInteger {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter three integers: ");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        int max = getMax(num1, num2, num3);

        System.out.println("Maximum input integer is " + max);
    }

    public static int getMax(int num1, int num2, int num3) { 
        if ((num1 >= num2) && (num1 >= num3)) 
            return num1;
        else if ((num2 >= num1) && (num2 >= num3)) 
            return num2;
        else if ((num3 >= num1) && (num3 >= num2)) 
            return num3;
    }
}

edit: editing this question to make it better after seeing the responses that this may be viewed as off-topic.

  1. The error message I'm getting is "missing return statement".

  2. I understand that there is a Math.max method of finding the maximum, but in this particular case, the task was given to convert to "if else" statements.

  3. There weren't any missing braces, brackets, and parentheses in my original code. They might have occurred in my copying of the code. Apologies for any confusion.

"tl;dr" version: Sorry for any mistakes, omissions or confusion caused by me.

Upvotes: 4

Views: 557

Answers (9)

Stugal
Stugal

Reputation: 880

This block:

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    else if ((num3 >= num1) && (num3 >= num2)) 
        return num3;
}

Requires an else statement with a return, or a return at the end of if/else block.

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

Java 8 solution for finding max from any number of inputs:

public static int maxOf(int... num) {
    return Arrays.stream(num).max().getAsInt();
}

Upvotes: 0

SENTHIL SARAVANAN
SENTHIL SARAVANAN

Reputation: 749

You can use Collections

public class MaxInteger {
 static List<Integer> list = new ArrayList<Integer>();  
 public static void main(String[] args) {
 System.out.print("Enter three integers: ");
    Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 3; i++) {        
      list.add(sc.nextInt());
    }
    Collections.sort(list); // Sort the arraylist
    System.out.println("Maximum input integer is " + list.get(list.size() - 1));
    } 
}  

Upvotes: 0

GhostCat
GhostCat

Reputation: 140525

The key point is making your paths easier to "read" by humans. That helps finding out why the compiler has a problem with your source code.

In your case: use braces for blocks. Always, even for one-liners, like in:

public static int getMax(int num1, int num2, int num3) { 
  if ((num1 >= num2) && (num1 >= num3)) {
    return num1;
  } else {
    if ((num2 >= num1) && (num2 >= num3)) {
      return num2;
    } else {
      if ((num3 >= num1) && (num3 >= num2)) {
      return num3;
    }
  }
}

Of course, many people would say "there is a lot of 'line noise' now"; but the point is: isn't it much more obvious now that your final if ... doesn't have an else? In other words: there is a path through your method that doesn't contain a return statement. And you just didn't see that because of the way you wrote down/formatted/indented your code!

Thus, the key lessons here are:

  1. Learn to read the compiler messages. They are really good in Java, and they are to be trusted!
  2. Structure your code in a way that clearly shows the paths within your methods.
  3. And of course, as others have correctly pointed out: don't re-invent the wheel. Java is coming with a ton of ready-to-use-well-tested libraries. Besides the learning effects, there is no sense in implementing your own min/max functionality.

Upvotes: 0

Adriaan Koster
Adriaan Koster

Reputation: 16209

How about this solution?

private static int getMax(int a, int b, int c) {
    int[] values = new int[]{a, b, c};
    Arrays.sort(values);
    return values[2];
}

Upvotes: 0

xenteros
xenteros

Reputation: 15842

Even though you know that this method will always return something, the compiler doesn't. The solution is to make the last else if an else as you know that this will still be logically correct.

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    return num3;
}

And one thing more! You're missing closing bracket of your class which I have added to your code snippet by editing the question as I qualified is as a typo. I assumed that you have it in your original code but I'm not sure now.

The above is about your compilation error. You can find some code review below.

Another thing is DRYing your code. Why should you repeat something that some one has written before? To calculate maximum of three numbers simply return:

public static int maxOfThree(int num1, int num2, int num3) { 
    return Math.max(Math.max(num1,num2),num3);
}

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521854

The reason for your compiler error appears to be that your getMax() method may not return a value in all cases. Change your code to this for immediate relief:

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    // otherwise num3 must be the greatest
    else return num3;
}

Better yet, use my implementation or the answer given by @Amy for even better results.

Just for fun you could actually determine the max of the three numbers using a single ternary statement:

public static int getMax(int num1, int num2, int num3) {
    int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3) ? num2 : num3;
    return max;
}

With regard to your current compiler error, the problem I saw is that you were missing a closing parenthesis around the class. Your code should take this form:

class MaxInteger {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter three integers: ");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        int max = getMax(num1, num2, num3);

        System.out.println("Maximum input integer is " + max);
    }

    public static int getMax(int num1, int num2, int num3) {
        int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3) ? num2 : num3;
        return max;
    }
}

Upvotes: 1

Amy
Amy

Reputation: 4032

Math.max() returns the highest from two values. you can apply this operation twice to get max out of three

int max =  Math.max(Math.max(num1,num2),num3);

Upvotes: 3

Danio
Danio

Reputation: 1114

In your getMax method, compiler is telling you that you must always return a result. You should add a case which will execute when none of youre if clauses match - add it on the end or under an else block.

Upvotes: 0

Related Questions