Reputation:
I'm doing an exercise where i have to write a method that returns the index of the largest element in an array.
The code is this:
import java.util.Scanner;
public class IndexLargestElement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 double values");
double[] list = new double [10];
for (int i = 0; i < list.length; i++)
list[i] = input.nextDouble();
int index = indexOfLargestElement(list);
System.out.println("The index of the largest element is " + index);
}
public static int indexOfLargestElement(double[] array){
double max = array[0];
int index = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
return index;
}
}
}
Now at int index=0; and max = array[i]; Netbeans tells me: This value is never used. Why? I don't understand what I did wrong? Probably something with the braces? When I try to run, it gives a compile error, saying there is no return statement.
Help is always appreciated! Thanks in advance :)
Upvotes: 0
Views: 673
Reputation: 357
the line "return index" should be outside the for loop.
public class IndexLargestElement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 double values");
double[] list = new double [10];
for (int i = 0; i < list.length; i++)
list[i] = input.nextDouble();
int index = indexOfLargestElement(list);
System.out.println("The index of the largest element is " + index);
}
public static int indexOfLargestElement(double[] array){
double max = array[0];
int index = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
}
}
return index;
}
Upvotes: 0
Reputation: 4506
public static int indexOfLargestElement(double[] array){
double max = array[0];
int index = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
}
}
return index;
Since you don't want to return before checking all elements, if it enters the if then max is never used.
Upvotes: 0
Reputation: 115328
Take a look on your indexOfLargestElement()
. Here is its simplified version:
for(.....) {
if(condition) {
return something;
}
}
This means that only if in one of the loop iterations condition will be true
the method will return value. But what if not? You have to make sure that method declared to return value does it in all scenarios.
In your case you have to decide what should you do if condition is always false. You can either return some kind of default value or throw exception. Do it at the end of your method:
int indexOfLargestElement() {
for(.....) {
if(condition) {
return something;
}
}
// RETURN
return SOME_DEFAULT;
// OR, alternatively throw exception:
// throw new IllegalArgumentException("Some text");
}
Upvotes: 1
Reputation: 679
You put "return index" inside if statement, which makes it unsure that the return will happen 100% (that's the warning you get), just move "return index" after the loop and if statement.
Upvotes: 0