16

Possible Duplicate:
Find the max of 3 numbers in Java with different data types (Basic Java)

Write a program that uses a scanner to read three integers (positive) displays the biggest number of three. (Please complete without using either of the operators && or ||. These operators will be covered in class shortly. Similarly loops are not required.)

Some sample run: 

Please input 3 integers: 5 8 3
The max of three is: 8

Please input 3 integers: 5 3 1
The max of three is 5

import java.lang.Math;
import java.util.Scanner;
public class max {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input 3 integers: ");
        String x = keyboard.nextLine();
        String y = keyboard.nextLine();
        String z = keyboard.nextLine();
        int max = Math.max(x,y,z);
        System.out.println(x + " " + y + " "+z);
        System.out.println("The max of three is: " + max);
    }
}   

I want to know what's wrong with this code and how I can find the max when I input 3 different values.

Community
  • 1
  • 1
user1730357
  • 355
  • 4
  • 7
  • 15
  • 2
    Two things: Change the variables `x`, `y`,`z` as `int` and call the `max` method as `Math.max(Math.max(x,y),z)` as it accepts two parameters only. Check the answer for fixed code snippet. – Yogendra Singh Oct 09 '12 at 05:02
  • Apache's [NumberUtils.max(int a, int b, int c)](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/NumberUtils.html) returns the maximum of three int values. – RKumsher Dec 05 '14 at 15:37
  • `Math.max` takes only two arguments, and those arguments must be numbers. So `Math.max(Math.max(Integer.valueOf(x), Integer.valueOf(y)), Integer.valueOf(z))` will solve this. – user3932000 Jan 13 '18 at 19:50

3 Answers3

38

Two things: Change the variables x, y, z as int and call the method as Math.max(Math.max(x,y),z) as it accepts two parameters only.

In Summary, change below:

    String x = keyboard.nextLine();
    String y = keyboard.nextLine();
    String z = keyboard.nextLine();
    int max = Math.max(x,y,z);

to

    int x = keyboard.nextInt();
    int y = keyboard.nextInt();
    int z = keyboard.nextInt();
    int max =  Math.max(Math.max(x,y),z);
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
2

It would help if you provided the error you are seeing. Look at http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html and you will see that max only returns the max between two numbers, so likely you code is not even compiling.

Solve all your compilation errors first.

Then your homework will consist of finding the max of three numbers by comparing the first two together, and comparing that max result with the third value. You should have enough to find your answer now.

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Lolo
  • 3,935
  • 5
  • 40
  • 50
2

You should know more about java.lang.Math.max:

  1. java.lang.Math.max(arg1,arg2) only accepts 2 arguments but you are writing 3 arguments in your code.
  2. The 2 arguments should be double,int,long and float but your are writing String arguments in Math.max function. You need to parse them in the required type.

You code will produce compile time error because of above mismatches.

Try following updated code, that will solve your purpose:

import java.lang.Math;
import java.util.Scanner;
public class max {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input 3 integers: ");
        int x = Integer.parseInt(keyboard.nextLine());
        int y = Integer.parseInt(keyboard.nextLine());
        int z = Integer.parseInt(keyboard.nextLine());
        int max = Math.max(x,y);
        if(max>y){ //suppose x is max then compare x with z to find max number
            max = Math.max(x,z);    
        }
        else{ //if y is max then compare y with z to find max number
            max = Math.max(y,z);    
        }
        System.out.println("The max of three is: " + max);
    }
} 
Arun Kumar
  • 6,534
  • 13
  • 40
  • 67