user2932587
user2932587

Reputation:

Arrays in Methods

I'm working on an assignment with the directions as follows,

Write a method that when passed an integer search value, and an array of integers will return true if the search value is in the array, otherwise returns false. Call the method and print the result in main.

Here is what I have so far,

public class ArrayLab {
  public static void main(String[] args) {
    int[] array = {15, -2, 18, -9, 90, 17, 981};
    int index;   
 boolean isThere = searchValue(15, array);
    System.out.println(isThere);

public static boolean searchValue(int search, int[] array) {
    Scanner input = new Scanner(System.in);
    boolean isThere = true;
    for(int index = 0; index < array.length; index++) {
          if (search == array[index])
           isThere = true; 
          else
            isThere = false; } // end for

 return isThere;
   } // end searchValue 

} // end class

The issues I"m having have varied depending on the several ways I've treid to rewrite this. As it is now, I'm not receiving any errors, but the value being returned is always false no matter what number I pass to the method from main.

Any suggestions as to how I can correct this issue would be very much appreciated.

Upvotes: 1

Views: 145

Answers (6)

Dylan Meeus
Dylan Meeus

Reputation: 5802

You need to break after the number has been found. Otherwise, you keep checking the other numbers as well. Thus, if the last number would return false, you'd just see "false"

for(int index = 0; index < array.length; index++) {
    if (search == array[index]) { 
        isThere = true;
        break; 
    } else
        isThere = false; 
    }
} 

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

1)Your given code won't compile since you wrote another method inside main method. That's an compiler error

2)You need to return from when you found the result. Now It's proceeding further and checking again.

With simplification, It's like

public static boolean searchValue(int search, int[] array) {
    for(int index = 0; index < array.length; index++) {
          if (search == array[index])
              return true;
     }      
     return false;
} 

Upvotes: 0

tkr_in
tkr_in

Reputation: 135

I see that if condition is successful then we should break the loop and return. That is missing

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201419

You should return true when you find the number. Of course, you should return false if you can't. So I think you want something like this

public static boolean searchValue(int search, int[] array) {
  if (array != null && array.length > 0) {
    for (int i : array) {
      if (i == search) {
        return true;
      }
    }
  }
  return false;
}

Upvotes: 0

Narendra Pathai
Narendra Pathai

Reputation: 41935

You are missing a break once you find that value matches.

Upvotes: 0

Siva
Siva

Reputation: 1940

Change your for loop as follows, you need o break the loop whenever you encounters the element,other wise the loop will continue till last element

for(int index = 0; index < array.length; index++) {
          if (search == array[index])
           isThere = true;
           break; 
          else
            isThere = false; } 

Upvotes: 0

Related Questions