2016-09-30 100 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)

编辑:我写此评论加入

前的代码
相关问题