2016-09-30 160 views
-4

我正在運行一個代碼,該代碼需要一個int列表和一個字符串列表,並將該數組的大小分別增加到適當的大小,然後使用不同的方法對數組進行排序同時還會找到重複的實例。代碼很好,直到我運行排序我的數組並查找重複的方法。我知道正確的輸出應該是什麼,並且應該沒有在intList中發現重複,並且在索引45788處的wordList中找到重複。我已經向其他人請求了執行這個相同簡單任務的幫助,並且與他們具有相同的代碼。我必須離開某個地方,但我找不到位置。我在命令提示符的輸出旁邊附加了兩個方法的照片。感謝您的幫助運行代碼時出現Java運行錯誤

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

public class Lab4 
{ 
    static final int INITIAL_CAPACITY = 10; 
    static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found 

    public static void main (String[] args) throws Exception 
    { 
     // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE 
     if (args.length < 1) 
     { 
      System.out.println("\nusage: C:\\> java Lab4 <numbers file> <words filename>\n\n"); // i.e. C:\> java Lab4 10000ints.txt 172822words.txt 
      System.exit(0); 
     } 

     String[] wordList = new String[INITIAL_CAPACITY]; 
     int[] intList = new int[INITIAL_CAPACITY]; 
     int wordCount = 0, intCount=0; 
     Scanner intFile = new Scanner(new File(args[0])); 
     BufferedReader wordFile = new BufferedReader(new FileReader(args[1])); 

     // P R O C E S S I N T F I L E 
     while (intFile.hasNextInt()) // i.e. while there are more ints in the file 
     { 
      if (intCount == intList.length) 
       intList = upSizeArr(intList); 
      intList[intCount++] = intFile.nextInt(); 

     } //END WHILE intFile 

     //close intfile 
     intFile.close(); 

     //output text with variables 
     System.out.format("%s loaded into intList array. size=%d, count=%d\n",args[0],intList.length,intCount); 

     int dupeIndex = indexOfFirstDupe(intList, intCount); 

     if (dupeIndex == NOT_FOUND) 
     { 
      System.out.format("No duplicate values found in intList\n"); 
     } 
     else 
     { 
      System.out.format("First duplicate value in intList found at index %d\n",dupeIndex); 
     } 

     // P R O C E S S S T R I N G F I L E 
     while (wordFile.ready()) // i.e. while there is another line (word) in the file 
     { 
      if (wordCount == wordList.length) 
       wordList = upSizeArr(wordList); 
      wordList[wordCount++] = wordFile.readLine(); 
     } //END WHILE wordFile 

     //closing wordfile 
     wordFile.close(); 

     //output text again with variables 
     System.out.format("%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount); 

     dupeIndex = indexOfFirstDupe(wordList, wordCount); 

     if (dupeIndex == NOT_FOUND) 
     { 
      System.out.format("No duplicate values found in wordList\n"); 
     } 
     else 
     { 
      System.out.format("First duplicate value in wordList found at index %d\n",dupeIndex); 

     } 
    } 

    // -------------------------------------------------------------------------------------------------------------------------------- 

    // method to double size of string array 

    static String[] upSizeArr(String[] fullArr) 
    { 
     int length = fullArr.length; 

     //creating a new array of double size 
     String[] upsizearr = new String[length*2]; 

      //this for loop assigns each old variable in fullArr 
      //and assigns it to the new larger array, upsizearr 
      for(int i = 0; i<length-1; i++) 
      { 
       upsizearr[i] = fullArr[i]; 
      } 

     return upsizearr; 
    } 


    // method to double size of int array 

    static int[] upSizeArr(int[] fullArr) 
    { 
     int length = fullArr.length; 

     //creating new array of double size 
     int[] upsizearr = new int[length*2]; 

      //this loop does the same as in upSizeArr method, 
      //assigning all values to new bigger array 
      for(int i = 0; i<length-1; i++) 
      { 
       upsizearr[i] = fullArr[i]; 
      } 

     return upsizearr; 
    } 


    // use Arrays.sort() before scanning for dupe 
    static int indexOfFirstDupe(int[] arr, int count) 
    {  
     Arrays.sort(arr); 
     int value = NOT_FOUND; 

     for(int i = (arr.length - count); i < count; i++) 
     { 
      if(arr[i] == arr[i-1]) 
      { 
       value = i; 
       break; 
      } 
     } 

     return value; 
    } 


    // use Array.sort() before scanning for dupe 
    static int indexOfFirstDupe(String[] arr, int count) 
    {  

     Arrays.sort(arr); 
     int value = NOT_FOUND; 

     for(int i = (arr.length - count); i < count; i++) 
     { 
      if(arr[i] == arr[i-1]) 
      { 
       value = i; 
       break; 
      } 
     } 

     return value; 
    } 

} // END CLASS 

[cmd and sorting/finding dupe arrays] [code where errors occur] 2

+0

如果它「運行錯誤」,它不能是「代碼的作品」和副相反 – Antoniossss

+0

添加您的代碼,而不是圖片的問題。 – TimeToCode

+2

您顯示了異常(在圖片中而不是複製和粘貼),然後甚至沒有顯示它引用的行(第144行,位於indexOfFirstDupe內)... – jonhopkins

回答

1

修訂回答:

經過深入研究後,看起來Arrays.sort()是責怪。一種可能性是數組大小通過「upSizeArr」增加的方式。或者,在將單詞添加到wordList數組時,wordFile.readLine()返回空值。無論原因是什麼,「countRunAndMakeAscending」錯誤主要是由於要排序的數組中的空值。

其他人遇到了這個問題,以及:

Sorting an array of strings in Java

的建議是使用一個ArrayList。

或者,循環訪問數組並在排序之前將任何空值設置爲非空值可以解決此問題。但是,您必須確定一個好的非空值候選,它在檢查「indexOfFirstDupe」方法中的模糊時不會損壞數據集。

因此,學習使用ArrayList可能是更簡單的路線。

離開舊的解決方案發布,因爲它解決了代碼中的一個單獨的問題。

老答案:

它看起來像通過單詞列表陣列循環當代碼遇到一個空值。在查看代碼的同時,似乎該問題也可能存在於int列表中。所以...有幾件事要糾正。通過在while循環中最後添加intCount和wordCount變量來改變您設置整數和單詞的方式。

當裝載整數到intList中......

// P R O C E S S I N T F I L E 
    while (intFile.hasNextInt()) // i.e. while there are more ints in the file 
    { 
     if (intCount == intList.length) 
      intList = upSizeArr(intList); 
     intList[intCount] = intFile.nextInt(); 
     intCount++; 
    } //END WHILE intFile 

當加載字到詞表

// P R O C E S S S T R I N G F I L E 
    while (wordFile.ready()) // i.e. while there is another line (word) in the file 
    { 
     if (wordCount == wordList.length) 
      wordList = upSizeArr(wordList); 
     wordList[wordCount] = wordFile.readLine(); 
     wordCount++; 
    } //END WHILE wordFile 
+0

我試過了,現在有個「countRunAndMakeAscending」的錯誤。與之前引用的相同的行仍然被引用 –

+0

這是軟件編程的故事,您修復了一個錯誤,然後轉移到下一個...沒有太多上下文,很難幫助您。即請在此主題中發佈更多信息,例如其他人請求的信息。 – haoudoin

+0

如果我的帖子回覆了你原來的帖子,請標記爲答覆!謝謝! :) – haoudoin

1

控制檯告訴我們錯誤在indexOfFirstDupe發生()上Arrays.sort()。你有兩種方法使用這個名稱,但由於該線運行良好,我們知道之後發生的錯誤:System.out.format("%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount);

所以誤差在indexOfFirstDupe(String[] arr, int count)

我看您已經導入java.util.* happing,所以Arrays.sort()應可用並且不會導致錯誤。我猜想'arr'是空的。嘗試使用System.out.println()Arrays.sort(arr)行打印arr到控制檯。如果它爲空,那就是你的問題。

0

如何閱讀例外:

異常線程 「main」 顯示java.lang.NullPointerException

此行的初學者唯一重要的部分是最後一部分(java.lang中。 NullPointerException)這是你的錯誤的類型。在這種情況下,你有一些對象,它是空的,你可以調用空對象上的方法。

在...

在...

在Lab4.IndexOfFirsDupe(Lab4.java:144)

在Lab4.main(Lab4.java:66)

這就是所謂的堆棧跟蹤。它告訴你代碼中的錯誤在哪裏。

的entrys包括三個重要信息:類(lab4),方法(IndexOfFirstDupe)和代碼行(行144)

編輯:我寫此評論加入

前的代碼