method problem

Hi!

I can't spot the compiler error for this code and i'm also not convinced that the method called below is going to work.

i want the output to be:

"The numbers that are below 100 are 69, 67, 2."

Can anyone help?thanks

class ReturnMaxNumber

{

publicstaticvoid main(String[] args)

{

int num1 = 69, num2 = 67, num3 = 488, num4 = 2;

System.out.println("The first number is " +num1);

System.out.println("The second number is " +num2);

System.out.println("The third number is " +num3);

System.out.println("The fourth number is " +num4);

System.out.println("");

System.out.println("The largest number is " +max(num1, num2, num3, num4));

System.out.println("The smallest number is " +min(num1, num2, num3, num4));

System.out.println("The numbers that are below 100 are " +below(num1, num2, num3, num4));

}

publicstaticint max(int x1,int x2,int x3,int x4)

{

int m = x1;

if (x2 > m) m = x2;

if (x3 > m) m = x3;

if (x4 > m) m = x4;

return m;

}

publicstaticint min(int x1,int x2,int x3,int x4)

{

int m = x1;

if (x2 < m) m = x2;

if (x3 < m) m = x3;

if (x4 < m) m = x4;

return m;

}

publicstaticint below(int x1,int x2,int x3,int x4)

{

if (x1 < 10)return x1;

if (x2 < 10)return x2;

if (x3 < 10)return x3;

if (x4 < 10)return x4;

}

}

ReturnMaxNumber.java:44: missing return statement

{

^

1 error

[3532 byte] By [mark_8206a] at [2007-11-26 18:05:57]
# 1
The error is very explicit. It's telling you exactly what the problem is. You have declared a method to return a value, but that method is completing without returning anything.
jverda at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 2
so the problem is with the below method?
mark_8206a at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 3

Take a look at the other two methods (excluding main) and then look at the bottom one. You notice a difference?

It is because in Java when you say public static int myMethod()

You must return an int.

otherwise you have to declare it as voidpublic static void myMethod()

"void" means that it does not return a value which is not what you want in this case.

Message was edited by:

kikemelly

kikemellya at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 4

If all the integers x1, x2, x3, x4 are >= 10, no value is returned, hence the error.

public static int below(int x1, int x2, int x3, int x4)

{

if (x1 < 10) return x1;

if (x2 < 10) return x2;

if (x3 < 10) return x3;

if (x4 < 10) return x4;

}

duckbilla at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 5
ok, so potentially all numbers could be greater than 10, is it possible to write code in that method that says "if all numbers are greater than 10 return nothing"? or is that totally wrong?
mark_8206a at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 6

yes, underneath your code just add

return null;

//or

return 0;

something like that. when you use if's that way your method still has to return even if none of the conditions match. like a default value.

kikemellya at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 7
Since you have the signaturepublic static int below(...)you must return an int value from that function. You might want to reconsider the logic of the function.#
duckbilla at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 8

If you know that it can never happen that all numbers >= 10, then you could do one of the following:

if (...) return ... ;

else if (...) return ...;

...

else return ...;

or keep it like you have now, but after the last line, add

throw new IllegalArgumentException("At least one input must be < 10");

jverda at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 9

> yes, underneath your code just add

> > return null;

>

> //or

>

> return 0;

>

>

>

> something like that. when you use if's that way your

> method still has to return even if none of the

> conditions match. like a default value.

You cannot return null if the return value is defined as int.

duckbilla at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 10
sorry is that under the line if (x4 < 10) return x4; ?
mark_8206a at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 11

if this time i wanted the below method to return all numbers below 100(therefore num1, num2, num4) 69, 67, 2

the method doesn't work

is it possible to get that method to return more than one integer?

you know what i mean?

public static int below(int x1, int x2, int x3, int x4)

{

if (x1 < 100) return x1;

if (x2 < 100) return x2;

if (x3 < 100) return x3;

if (x4 < 100) return x4;

return 0;

}

mark_8206a at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 12

You can only return one value in Java. You could however return a number, say "39" (or any other number based on the arguments) and then deal with this number in another method. Like you could call one method, if nothing matches then return "39" and in your calling code or other code you could say print all the numbers less than "39" or whatever the value might be.

in short deal with this in another method based on one value from your below method.

kikemellya at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 13

> if this time i wanted the below method to return all

> numbers below 100(therefore num1, num2, num4) 69,

> 67, 2

>

> the method doesn't work

> is it possible to get that method to return more than

> one integer?

No.

If you want to return "all the integer args < 100" then you can return an array.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

jverda at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...
# 14

if (x1 < 10) return x1;

if (x2 < 10) return x2;

if (x3 < 10) return x3;

if (x4 < 10) return x4;

If you are trying to find numbers less than 100 (ignoring the fact you can only return one value for now) shouldn't you change to 10 to 100?

floundera at 2007-7-9 5:36:47 > top of Java-index,Java Essentials,New To Java...