title: Linear search returning not found when it should be returning subscript number
body: <p>I'm working on a homework assignment for Java, where a program is supposed to read in 10 numbers from the user, then ask for a number to search. It sorts the numbers (ascending) and performs a linear search on the array, then should either return the subscript it was found at or a message saying not found. </p>
  
  <p>My code is giving me the "not found" response even when the subscript should be returned. Can you take a look at my code? I've tried altering my if statement and it didn't help. I've used the book examples of the search and sort methods as closely as I could (feel free to point out errors).</p>
  
  <p>Here is the code.</p>
  
  <pre><code>package program11;
  import java.util.Scanner;
  
  public class ArraySearch {
  
      public static void main(String[] args) {
          double[] arrayBuild = new double[10];
          Scanner input = new Scanner(System.in);
  
          for (int i = 0; i &lt; arrayBuild.length; i++) {
              System.out.print("Enter a number.");
              arrayBuild[i] = input.nextDouble();
          }
  
          System.out.print("Enter a number to search for ");
          int objective = input.nextInt();
  
          linearCheck(arrayBuild, objective);
  
          if ((objective &gt;= 0) &amp;&amp; (objective &lt; arrayBuild.length)) {
              System.out.println("Found at index: " + objective);
          } else {
              System.out.println("Not Found");
          }
      }
  
      public static void arraySort(double[] arrayBuild) {
          for (int i = 1; i &lt; arrayBuild.length; i++) {
              double currentPoint = arrayBuild[i];
              int r;
              for (r = i - 1; r &gt;= 0 &amp;&amp; arrayBuild[r] &gt; currentPoint; r--) {
                  arrayBuild[r + 1] = arrayBuild[r];
              }
              arrayBuild[r + 1] = currentPoint;
          }
      }
  
      public static double linearCheck(double[] arrayBuild, int objective) {
          for (int i = 0; i &lt; arrayBuild.length; i++) {
              if (objective == arrayBuild[i])
                  return i;
          }
          return -1;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT- new code. It's all complete now, except when I enter a number that is not in the array I get a negative result. For example, entering 10 8 7 6 5 3 5 3 5 3 5 6 and searching for 11 gives me the result of -1 for the linear search and -11 for the binary search. I took your advice as well as I could. What am I missing now?  </p>
  
  <pre><code>package program11;
  import java.util.Scanner;
  import javax.swing.JOptionPane;
  public class ArraySearch {
  
      public static void main(String[] args) {
          double[] arrayBuild = new double[10];
          Scanner input = new Scanner(System.in);
          int reply = 2;
  
  for (int i = 0; i &lt; arrayBuild.length; i++) {
      System.out.print("Enter a number.");
      arrayBuild[i] = input.nextDouble();
          }
  while (reply != 1) {
          System.out.print("Enter a number to search for ");
          double objective = input.nextDouble();
  
  arraySort(arrayBuild);
          double linearResult = linearCheck(arrayBuild, objective);
  
  if (objective &gt;= 0) {
          System.out.println("Linear search found at index: " + linearResult);
                  }
  else {
          System.out.println("Not Found (linear)");
          }
          double binaryResult = binaryCheck(arrayBuild, objective);
  
  if (objective &gt;= 0) {
  System.out.println("Binary search found at index: " + binaryResult);
                  }
  else {
       System.out.println("Not Found (binary)");
          }
  reply = JOptionPane.showConfirmDialog(null, "Continue?"); 
      }
  }
  
  public static void arraySort(double[] arrayBuild) {
      for (int i = 1; i &lt; arrayBuild.length; i++) {
          double currentPoint = arrayBuild[i];
          int r;
          for (r = i - 1; r &gt;= 0 &amp;&amp; arrayBuild[r] &gt; currentPoint; r--) {
              arrayBuild[r + 1] = arrayBuild[r];
          }
          arrayBuild[r + 1] = currentPoint;
      }
      }
  
  public static double linearCheck(double[] arrayBuild, double objective) {
      for (int i = 0; i &lt; arrayBuild.length; i++) {
          if (objective == arrayBuild[i])
              return i;
      }
  return -1;
      }
  public static double binaryCheck(double[] arrayBuild, double objective) {
      int low = 0;
      int high = arrayBuild.length - 1;
  
      while (high &gt;= low) {
          int mid = (low + high) / 2;
          if (objective &lt; arrayBuild[mid])
              high = mid - 1;
          else if (objective == arrayBuild[mid])
              return mid;
          else
          low = mid + 1;
      }
  return -low - 1;
      }
  }
  </code></pre>