Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java我的程序的数组没有显示任何值_Java_Arrays_Eclipse - Fatal编程技术网

Java我的程序的数组没有显示任何值

Java我的程序的数组没有显示任何值,java,arrays,eclipse,Java,Arrays,Eclipse,我正在写一个程序来搜索一个字符串,并告诉我是否找到了它 import java.io.*; public class BinarySearchTest { public static void main(String [] args) throws IOException { int result; String searchValue; String input; // An array of numbers to search

我正在写一个程序来搜索一个字符串,并告诉我是否找到了它

import java.io.*;


public class BinarySearchTest
{
   public static void main(String [] args) throws IOException
   {
      int result;
      String searchValue;
      String input;

      // An array of numbers to search.
      String[] numbers = {"Jake", "Jerry", "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"};

      // Create the console input objects.
      InputStreamReader reader =
                 new InputStreamReader(System.in);
      BufferedReader keyboard =
                 new BufferedReader(reader);

      // First we must sort the array in ascending order.
      IntQuickSorter.quickSort(numbers);

      do
      {
         // Get a value to search for.
         System.out.print("Enter a value to search for: ");
         input = keyboard.readLine();
         searchValue = input;

         // Search for the value
         result = IntBinarySearcher.i;

        // Display the results.
        if (result == -1)
           System.out.println(searchValue + " was not found.");
        else
        {
           System.out.println(searchValue + " was found at " +
                              "element " + result);
        }

        // Does the user want to search again?
        System.out.print("Do you want to search again? (Y or N): ");
        input = keyboard.readLine();
      } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y');
   }
}




public class IntBinarySearcher
{
   static int i;





   public static int search(String[] numbers, String value)
   {
      int first;       // First array element
      int last;        // Last array element
      int middle;      // Mid point of search
      int position;    // Position of search value
      boolean found;   // Flag     

      // Set the initial values.
      first = 0;
      last = numbers.length - 1;
      position = -1;
      found = false;

      setI(Integer.parseInt(value));

      // Search for the value.
      while (!found && first <= last)
      {
         // Calculate mid point
         middle = (first + last) / 2;

         // If value is found at midpoint...
         if (numbers[middle].equals(value))
         {
            found = true;
            position = middle;
         }

         // else if value is in lower half...
         // need tell is value is less then the integer?, with out using equality regulators
         else if (value.compareTo(numbers[middle]) < 0)
            last = middle - 1;
         // else if value is in upper half....
         else
            first = middle + 1;
      }

      // Return the position of the item, or -1
      // if it was not found.
      return position;
   }

    public static void setI(int i) 
    {
        IntBinarySearcher.i = i;
    }

    public static int getI() 
    {
        return i;
    }
}






public class IntQuickSorter
{


   public static void quickSort(String[] numbers)
   {
      doQuickSort(numbers, 0, numbers.length - 1);
   }



   private static void doQuickSort(String[] numbers, int start, int end)
   {
      int pivotPoint;

      if (start < end)
      {
         // Get the pivot point.
         pivotPoint = partition(numbers, start, end);

         // Sort the first sub list.
         doQuickSort(numbers, start, pivotPoint - 1);

         // Sort the second sub list.
         doQuickSort(numbers, pivotPoint + 1, end);
      }
   }



   private static int partition(String[] numbers, int start, int end)
   {
      String pivotValue;    // To hold the pivot value
      int endOfLeftList; // Last element in the left sub list.
      int mid;           // To hold the mid-point subscript

      // Find the subscript of the middle element.
      // This will be our pivot value.
      mid = (start + end) / 2;

      // Swap the middle element with the first element.
      // This moves the pivot value to the start of 
      // the list.
      swap(numbers, start, mid);

      // Save the pivot value for comparisons.
      pivotValue = numbers[start];

      // For now, the end of the left sub list is
      // the first element.
      endOfLeftList = start;

      // Scan the entire list and move any values that
      // are less than the pivot value to the left
      // sub list.
      for (int scan = start + 1; scan <= end; scan++)
      {
         if (pivotValue.compareTo(numbers[scan])< 0) // pivotValue.compareTo(numbers[scan])< 0)
         {
            endOfLeftList++;
            swap(numbers, endOfLeftList, scan);
         }
      }

      // Move the pivot value to end of the
      // left sub list.
      swap(numbers, start, endOfLeftList);

      // Return the subscript of the pivot value.
      return endOfLeftList;
   }

   /**
      The swap method swaps the contents of two elements
      in an int array.
      @param The array containing the two elements.
      @param a The subscript of the first element.
      @param b The subscript of the second element.
   */

   private static void swap(String[] numbers, int a, int b)
   {
      String temp;

      temp = numbers[a];
      numbers[a] = numbers[b];
      numbers[b] = temp;
   }
}

您似乎没有执行搜索。你需要排队:

IntBinarySearcher.search(numbers, searchValue);
result = IntBinarySearcher.i;
行前:

IntBinarySearcher.search(numbers, searchValue);
result = IntBinarySearcher.i;
任何地方都不能调用IntQuickSorter.search。以后再说吧

searchValue = input;

缺少了一些东西:

searchValue = input;

//IntBinarySearcher.search(numbers, searchValue); here?

// Search for the value
result = IntBinarySearcher.i;
编辑:

由于IntBinarySearcher.i设置不正确,因此也无法正常工作。请参见Edit2:

更好:

result = IntBinarySearcher.search(numbers, searchValue);
编辑2:


哦,这将抛出value=Bill的NumberFormatException:setIInteger.parseIntvalue

我看不到您的代码中有调用IntBinarySearcher.search的地方。如果没有,我将不会被设置为任何值,对吗?

例如,如果输入bill,请输入一个要搜索的值:bill bill在元素0处找到。那么您的问题是什么?如果您有任何错误,请准确说明错误消息。如果输出不正确,请说明预期输出和实际输出。您给出的示例听起来是正确的:Bill应该是排序后数组中的第一个元素。这是相当多的代码,并且没有太多关于您怀疑问题所在的描述。这可能有助于将其缩减为一个简短的10行示例,对于一个相对简单的问题,这应该是简单的,甚至可能导致您解决自己的问题!看,杰克、杰瑞和比尔的号码是什么?访问静态字段搜索值的方式是什么?这不是一个错误,我的程序说用户输入的都是元素0。我的程序找不到与元素位置匹配的字符串输入。