Reputation: 61
public class ArrayPrac {
public static void main(String[] args) {
int[] arrayOne = {2, 3, 4, 5, 6};
System.out.println(findMin(arrayOne));
}
public static void findMin(int[] list) {
int minValue = list[0];
int i = 1;
for( i = 1; 1 < list.length; i++);
if(list[i] < minValue) {
minValue = list[i];
}
}
}
In the System.out.print part in line 6 it wont run and gives me the compiler error:
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
I seem to have searched for an answer all day, so now I post my specific case.
Cheers.
Upvotes: 1
Views: 103
Reputation: 236064
Fix this, at the end of your findMin()
method you must return the minimum value that was found:
return minValue;
And consequently, the method signature must be changed, too:
public static int findMin(int[] list)
It makes sense: if the findMin()
method does all that hard work to find the minimum value, the end result must not be left as a local variable, it won't be useful outside if you don't return it after the method invocation ends.
There's another hard-to-find bug lurking, by the way. Remove the ;
at the end of the line with the for
, and put the contents of the loop inside a pair of {}
. Currently, the loop is empty, and the lines after the for
lie outside the loop. And the loop condition is wrong, too! here's how the method should look after all the problems are fixed:
public static int findMin(int[] list) {
int minValue = list[0];
for (int i = 1; i < list.length; i++) {
if (list[i] < minValue) {
minValue = list[i];
}
}
return minValue;
}
Upvotes: 7
Reputation: 425168
The method findMin()
is declared as returning type void
: Either declare it to return something (and return something),
public static int findMin(int[] list) {
int minValue = list[0];
int i = 1;
for( i = 1; 1 < list.length; i++)
if(list[i] < minValue) {
minValue = list[i];
}
return minValue;
}
Note fixing bug: removing semicolon from after for()
Upvotes: 0
Reputation: 68715
System.out.println
takes a String
input but you are passing a void
. As your method findMin
returns void. This is causing the compiler error.
Now speaking about the logical problem, you may want to display the output of findMin method but the method does not return anything. So returning minValue
may make sense here.
Once you return the int
value from minValue
method then you can display the result by concatenating it to a an empty string. Something like this:
System.out.println("" + findMin(arrayOne));
Upvotes: 2