2011-03-17 20 views
0

我正在編寫一個程序來搜索一個字符串,並告訴我它是否被發現。Java我的程序的數組沒有顯示任何值

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; 
    } 
} 
+0

如果輸入賬單例如輸入一個值來搜索:Bill Bill找到了元素0. – user663428 2011-03-17 16:20:22

+0

那麼你的問題是什麼?如果您有任何錯誤,請詳細說明錯誤消息。如果輸出錯誤,請描述預期的和實際的輸出。您給出的示例聽起來正確:「Bill」應該是數組排序後的第一個元素。 – 2011-03-17 16:22:47

+0

這是相當多的代碼,並沒有太多的描述你懷疑問題的地方。這可能有助於將其縮減爲一個簡短的(也許是10行)示例,這對於相對簡單的問題應該是直截了當的,甚至可能導致您解決自己的問題!見[sscce.org](http://sscce.org/)。 – 2011-03-17 16:23:34

回答

5

有東西丟失:

searchValue = input; 

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

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

編輯:

這不會工作,要麼因爲IntBinarySearcher.i是從來沒有 「正確」 設置(見EDIT 2):)。

更好:

result = IntBinarySearcher.search(numbers, searchValue); 

編輯2:

哦,這將拋出值= 「條例」 NumberFormatException異常:setI(Integer.parseInt(value));

2

您似乎沒有執行搜索。您需要行:

IntBinarySearcher.search(numbers, searchValue); 

前行:

result = IntBinarySearcher.i; 
+0

好吧,這是我的問題,謝謝。 – user663428 2011-03-17 16:26:33

0

你不叫IntQuickSorter.search()任何地方。說它是後

searchValue = input; 
0

我沒有在你的代碼看看你正在調用IntBinarySearcher.search。沒有這個我不會被設置爲任何東西,對吧?

相關問題