-3

I'm trying to find out why I'm getting an error compiling my code. It should be finding the maximum and minimum of the variables entered in the for loops. I know the code is redundant, but it's for a class.

   import java.util.*;
   public class ForInputNumbers
   {
      public static void main (String[] args)
      {
      Scanner input = new Scanner(System.in);      
      int value1=0,value2=0,value3=0,value4=0,value5=0;

      System.out.println("Please type a number");

      for (; value1==0;System.out.println("Please type the next number"))
      {
      value1 = input.nextInt();
      }

      for (; value2==0;System.out.println("Please type the next number"))
      {
      value2 = input.nextInt();
      }

      for (; value3==0;System.out.println("Please type the next number"))
      {
      value3 = input.nextInt();
      }

      for (; value4==0;System.out.println("Please type the next number"))
      {
      value4 = input.nextInt();
      }

      for (; value5==0;System.out.println("Please type the next number"))
      {
      value5 = input.nextInt();
      }

   System.out.println("Your numbers: "+value1+" "+value2+" "+value3+" "+value4+" "+value5);

   System.out.println("The sum of your numbers: "+(value1+value2+value3+value4+value5));

   System.out.println("The average of your numbers: "+(value1+value2+value3+value4+value5)/5);

   System.out.println("The largest of your numbers: "+(Math.max(value1,value2,value3,value4,value5)));

   System.out.println("The smallest of your numbers: "+(Math.min(value1,value2,value3,value4,value5)));

      }//end main method
   }//end class

My errors:

ForInputNumbers.java:60: error: no suitable method found for max(int,int,int,int,int)
   System.out.println("The largest of your numbers: "+(Math.max(value1,value2,value3,value4,value5)));
                                                           ^
    method Math.max(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(double,double) is not applicable
      (actual and formal argument lists differ in length)
ForInputNumbers.java:62: error: no suitable method found for min(int,int,int,int,int)
   System.out.println("The smallest of your numbers: "+(Math.min(value1,value2,value3,value4,value5)));
                                                            ^
    method Math.min(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(double,double) is not applicable
      (actual and formal argument lists differ in length)
2 errors

Any help is appreciated, thanks.

3 Answers3

4

Math.max and Math.min only accept pairs of arguments. You can't pass more than 2 parameters.

The neatest way to do this would be to wrap the values into a List<Integer> and then use Collections.max and Collections.min:

List<Integer> values = Arrays.asList(value1, value2, value3, value4, value5);
System.out.println("The largest of your numbers: "+Collections.max(values));
System.out.println("The smallest of your numbers: "+Collections.min(values));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
3

Math.max() only takes 2 arguments, so you have to nest them to find the max of more numbers. In this case it would look like Math.max(value1, Math.max(value2, Math.max(value3, Math.max(value4, value5)))) You will also have to do something similar to this with your Math.min() function. Ideally, you would use an array or list, but it isn't necessary in this case.

Natecat
  • 2,175
  • 1
  • 17
  • 20
  • You've missed a bracket and a semi colon off your code. – Neil Twist Apr 11 '16 at 22:43
  • 1
    @NeilTwist - Actually, a semicolon would be incorrect. This is an expression not a statement. (Syntactically, it could be a statement, but in context it makes zero sense to calculate the max of 4 numbers and then throw the result away. Even out of context ... IMO.) – Stephen C Apr 11 '16 at 22:46
  • @StephenC To be honest, the rest of the code is hardly best practice. – Neil Twist Apr 11 '16 at 22:51
  • 1
    @NeilTwist - Which code? Code in the Question or code in the Answer. If you are talking about the Answer, I disagree. (Unless you are using "best practice" as shorthand for code that you "like" ... which a lot of people do :-) ) – Stephen C Apr 11 '16 at 22:55
  • @StephenC the question! Though the answer is no better as this effectively hardcodes the number of values to five, making changes much harder later and potentially replicating code to forget to change later. It could potentially introduce a nasty bug at some point in the future if it were production code. – Neil Twist Apr 11 '16 at 23:00
  • 2
    @NeilTwist The number of values is already hardcoded to 5 by the asker, and this project is so small that reusing code isn't necessary. Even if this was a large project, if you ever used a function like this, you wouldn't need to ever change the number of values. What nasty bug could code like this possibly introduce? – Natecat Apr 11 '16 at 23:04
  • @Natecat you've clearly never worked on a 500k LoC code base where people have done this sort of thing. – Neil Twist Apr 11 '16 at 23:07
  • 1
    @NeilTwist - Why do you say that? Maybe he has. But anyway, it is not the size of the codebase that determines whether the "hard-wired" approach is correct. The true determinant is whether the assumption that you can hard-wire is actually correct. To be honest, unless we see the requirements that the OP was given by his teacher, we cannot make that judgment. – Stephen C Apr 12 '16 at 00:23
  • @StephenC "NateCat": _Even if this was a large project, if you ever used a function like this, you wouldn't need to ever change the number of values. What nasty bug could code like this possibly introduce?_ is what my comment referred to. I hope I don't have to explain any further. I believe that the extra 30 seconds taken to make a recursive function/method would be worth it just to get in the habit of doing it. I have often seen coders write "quick" fixes, only to have to re-write them later, which also then has unintended consequences as someone else started using it in the meantime. – Neil Twist Apr 12 '16 at 09:58
  • 1
    @NeilTwist - Please bear in mind the context here. This is clearly a programming exercise done by a beginner. The aim is not write elegant, production quality code. The aim is to write a *simple* program ... and get it to work. These lessons you are trying to teach are probably beyond a newbie programmer's ability to understand. (No disrespect intended!) – Stephen C Apr 12 '16 at 10:55
0

EDIT: the Answer by Andy Turner is probably the best.


Original Answer:

Math.min and Math.max both expect two number types, so you will need to either call them one after the other keeping the min or max each time, or preferably you will create a recursive utility function that will accept an array and return the min or max value.

Or for a one liner just as an example:

Math.max(value1, Math.max(value2, Math.max(value3, Math.max(value4, value5))));
Neil Twist
  • 1,099
  • 9
  • 12
  • Generally, java style guides suggest you put whitespace after each comma. – Natecat Apr 11 '16 at 22:40
  • @Natecat let's be fair, they would also say you definitely shouldn't be doing this sort of thing that is limited to this exact use case either. Ideally you would have an array and reduce it. – Neil Twist Apr 11 '16 at 22:41
  • He doesn't have an array though, and the idiom to find the max of more than 2 numbers that aren't in an array is to nest the functions. And I was simply suggesting you edit your post and include whitespace – Natecat Apr 11 '16 at 22:46
  • @Natecat I'd definitely disagree there, nesting functions like that just makes the code unreadable. Andy Turner hits the right spot with his answer. – Neil Twist Apr 11 '16 at 22:53
  • @NeilTwist, not necessarily the best. It is a lot cleaner. However, maybe it's not what the teacher is looking for (this does seem like an assignment question). – robotlos Apr 11 '16 at 22:53
  • @robotlos reading it back, he does say it's for a class, which I assumed was a Java class, but it's a school class, not how we'd refer to it in Britain so I was confused. I would hope that the teacher was looking for some recursion, not just random nesting that isn't maintainable – Neil Twist Apr 11 '16 at 22:56