2012-07-08 35 views
0

美好的一天!這個程序應該排序文件中的前n個單詞。當我調用mergeSort_srt方法時,請幫助我傳遞參數。當我運行這個時,控制檯說合併排序中的參數傳遞

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method mergeSort_srt(int[], int, int) in the type SortingAnalysis is not applicable for the arguments (String[], int, int) 

我是新來編程expecially在Java語言,我很困惑。請幫幫我。儘管我想自己找到這個錯誤,但我不能,因爲我對這些東西一無所知,我需要真實的人幫助,而不僅僅是通過在線閱讀教程。非常感謝你!

import java.io.*; 
import java.util.*; 

public class SortingAnalysis { 

    public static void mergeSort_srt(int array[],int lo, int n){ 
     int low = lo; 
     int high = n; 
     if (low >= high) { 
      return; 
     } 

     int middle = (low + high)/2; 
     mergeSort_srt(array, low, middle); 
     mergeSort_srt(array, middle + 1, high); 
     int end_low = middle; 
     int start_high = middle + 1; 
     while ((lo <= end_low) && (start_high <= high)) { 
      if (array[low] < array[start_high]) { 
       low++; 
      } else { 
       int Temp = array[start_high]; 
       for (int k = start_high- 1; k >= low; k--) { 
        array[k+1] = array[k]; 
       } 
       array[low] = Temp; 
       low++; 
       end_low++; 
       start_high++; 
      } 
     } 
    } 

    public static void main(String[] args) { 
     final int NO_OF_WORDS = 10000; 
     try { 
      Scanner file = new Scanner(new File(args[0])); 
      String[] words = new String[NO_OF_WORDS]; 

      int i = 0; 
      while(file.hasNext() && i < NO_OF_WORDS) { 
       words[i] = file.next(); 
       i++; 
      } 
      long start = System.currentTimeMillis(); 

      mergeSort_srt(words, 0, words.length-1); 
      long end = System.currentTimeMillis(); 
      System.out.println("Sorted Words: "); 
      for(int j = 0; j < words.length; j++) { 
       System.out.println(words[j]); 
      }  
      System.out.print("Running time: " + (end - start) + "ms"); 

     } 
     catch(SecurityException securityException) { 
      System.err.println("You do not have proper privilege to access the files."); 
      System.exit(1); 
     } 
     catch(FileNotFoundException fileNotFoundException) { 
      System.err.println("Error accessing file"); 
      System.exit(1); 
     } 
    } 
} 

回答

4

基本上這個錯誤是說什麼是你有一個方法,在某個地方叫做quicksort。 quicksort方法以一個字符串(String [])和兩個整數作爲參數。但是在你的代碼中,你試圖用SortingAnalysis類型的對象來調用它。由於編譯器無法找到一種叫做quicksort的方法,它會引發這種錯誤。

儘管由於在發佈的代碼中找不到調用方法quicksort的任何調用,我必須假定代碼或錯誤消息已過期。

EDIT由於OP代碼編輯

現在,你的代碼是正確的,這是很明顯的問題是什麼。您的方法mergeSort_srt被聲明爲:mergeSort_srt(int array[],int lo, int n)。它期望在第一個參數中有一個整數數組。在你的主要方法中,你這樣稱呼它:mergeSort_srt(words, 0, words.length-1);,其中words字符串的數組,而不是整數。

要解決這個問題,你需要更新你的mergeSort_srt方法:

  1. 採取字符串數組(字符串[])作爲輸入
  2. 更新排序邏輯來處理字符串,如它現在都是爲整數或其他數字編寫的。
+0

哎呀,我很抱歉,我只是從另一個窗口複製粘貼錯誤。感謝您的認可!但那真的是mergeSort_srt。我真正的問題是參數的傳遞。 – 2012-07-08 13:13:55

+0

@FirstLady,我根據您的更改更新了我的答案。 – 2012-07-08 13:21:14