Java 运行 有效代码出错

Java Run Error on code that works

我正在 运行ning 一个代码,它接受一个整数列表和一个字符串列表,分别将数组的大小增加到适当的大小,然后用不同的方法对数组进行排序同时还找到重复的实例。代码很好,直到我 运行 对我的数组进行排序并查找重复项的方法。我知道正确的输出应该是什么,并且在 intList 中不应该找到重复项,在索引 45788 处的 wordList 中不应该找到重复项。我已经向其他人寻求帮助来完成同样的简单任务,并且拥有与他们相同的代码。我必须离开某个地方,但我找不到地方。我在命令提示符的输出旁边附上了这两种方法的照片。感谢您的帮助

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

[] []2

控制台告诉我们 Arrays.sort() 上的 indexOfFirstDupe() 中发生了错误。您有两个具有此名称的方法,但由于此行运行良好,我们知道错误发生在它之后: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.*,所以 Arrays.sort() 应该可用并且不会导致错误。我猜 'arr' 是空的。尝试在带有 Arrays.sort(arr) 的行之前使用 System.out.println()arr 打印到控制台。如果它为空,那是你的问题。

如何读取异常:

Exception in thread "main" java.lang.NullPointerException

对于初学者来说,这行唯一重要的部分是最后一部分(java.lang.NullPointerException),这是您的错误类型。在这种情况下,您有一些空对象,您尝试在空对象上调用方法。

at ...

at ...

at Lab4.IndexOfFirsDupe(Lab4.java:144)

at Lab4.main(Lab4.java:66)

这就是所谓的堆栈跟踪。它会告诉您代码中的错误所在。

条目包含三个重要信息:class (lab4)、方法 (IndexOfFirstDupe) 和代码中的行(第 144 行)

编辑:我在添加代码之前写了这篇评论

修改后的答案:

在更仔细地调查之后。看来 Arrays.sort() 是罪魁祸首。一种可能性是数组大小增加 "upSizeArr" 的方式。或者 wordFile.readLine() 在向 wordList 数组添加单词时返回空值。不管是什么原因,"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

将单词加载到 wordList 时

    // 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