0

public static boolean linearSearch(int[] array, int target): This method should take as input an array of int as well as an int . It should return true if the element target is present in array . It should do so by examining each element of array one at a time sequentially starting

from the beginning of the array until the end. The method should return false if the element is not present.

public class ArrayUtilities{
  public static void main(String[] args){
    int[] array1= {1,2,3,4,5};
    int target1 = 2;
    linearSearch(array1,target1);
  }

    public static boolean linearSearch(int[] array, int target){
      int x=0;
      for(int i = 0; i< array.length; i++){
        if(array[i] == target){
          return true;
    }
        else{
          x++;
        }
      }
      if(x == 0){
        return false;
      }
      return linearSearch;//**error here!**
}
}

Here is the code i wrote for this question. however i get a mistake at return linearSearch. I dont understand why. thank you in advance!

the mistake says: Symbol: variable linearSearch Location: class ArrayUtilities

2
  • 2
    What mistake? What error? What the hammer? What the chain? Commented Oct 29, 2013 at 20:42
  • That is not a cryptic error message. It tells you that it doesn't know what linearSearch is. Commented Oct 29, 2013 at 20:45

5 Answers 5

2

It's good that you are immediately returning true if you find a match.

But I don't understand the purpose of the x variable. I would remove it entirely (and remove the else from the if).

If you finish the for loop, then you haven't found it, and you can return false right there. The line return linearSearch doesn't make any sense. Have just one line after the for loop ends: return false;.

Sign up to request clarification or add additional context in comments.

3 Comments

"It should do so by examining each element of array one at a time sequentially starting" i have to iterate every value i think.
But once you've found it, it will always be true, regardless of the rest of the content, so you can stop there and return true, as you already have done.
You are iterating all the values with the for loop already, x does nothing except for maybe counting how many iterations you have gone through, but then you are not using that information for anything.
1

Why a conditional "else" at the end? if your loop ends without finding any match, returns false, just returns true whenever you find a match. I mean, as soon as you find the match, return true, if you end the iteration whithout finding anything, return false.

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

Comments

0

The return statement needs to have a boolean value or variable after it. linearSearch (without any parameters) is neither of those.

Comments

0

Decide what you want to return, i see that you are returning true, ie., boolean, also you are trying to return the linear search, i dont know what that is. It has to be either true or falsee.

Comments

0
public boolean check;
public static boolean linearSearch(int[] array, int target){
    check = false;
    for(int i = 0; i < array.length; i++){
        if(array[i] == target){
            check = true;
        }
    }
    return check;
}

1 Comment

Code without really explaining what's wrong with the OPs solution, is rather unhelpful. Either he'll not understand what you're doing, or he'll just copy it directly. Neither of which are in the OPs best interest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.