java - 文件排序和插入排序。线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 0

标签 java file file-io insertion-sort

该程序使用插入排序对文件中的前 n 个单词进行排序

这不是我做的。我们被要求使用老师提供的这个程序来实现其他排序技术。我导入了源代码并运行它。它说:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at SortingAnalysis.main(SortingAnalysis.java:26)

但是当我们老师在类里面演示时,并没有错误。

我还想知道它如何对文件中的单词进行排序,甚至无需说明文件名(例如 tobesorted.txt)。也许只要它在 JRE 系统库中,它就可以工作,不是吗?

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

/**
 * Compares the running times of sorting algorithms
 * @author bryann
 *
 */
public class SortingAnalysis {

    public static void insertionSort(String[] a) {
        int n = a.length;
        for(int i = 1; i < n; i++) {
            String cur = a[i];
            int j = i - 1;
            while((j >= 0) && (a[j].compareTo(cur) > 0)) {
                a[j + 1] = a[j--];
            } // end while
            a[j + 1] = cur;
        } // end for
    } // end insertionSort

    public static void main(String[] args) {
        final int NO_OF_WORDS = 5000;
        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++;
            } // end while
            long start = System.currentTimeMillis();
            insertionSort(words);
            long end = System.currentTimeMillis();
            System.out.println("Sorted Words: ");
            for(int j = 0; j < words.length; j++) {
                System.out.println(words[j]);
            } // end for        
            System.out.print("Running time of insertion sort: " + (end - start) + "ms");

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

错误是因为导入吗?使用Eclipse,我只是点击了
文件>导入>常规>文件系统>来自目录(他发送给我们的整个文件夹)>进入文件夹(我创建了一个新项目,在那里我“导入” ”代码)>完成

请帮助我。我无法开始分配(即在同一源文件中尝试其他排序技术),因为我无法运行它。非常感谢!

最佳答案

程序的主函数需要参数:

public static void main(String[] args) {

这些参数是在命令行上传递给程序的参数:

java SortingAnalysis /home/somebody/tobesorted.txt

在这种情况下,args[0] 将是 "/home/somebody/tobesorted.txt"

这使程序知道要打开什么文件:

Scanner file = new Scanner(new File(args[0]));

但是,当您在不提供文件路径的情况下启动程序时,args 太短,并且您会遇到此 java.lang.ArrayIndexOutOfBoundsException: 0 as args[0] does not exist 您得到.

因此请给出要排序的文件的路径以消除此错误。例如:

java SortingAnalysis C:\somepath\tobesorted.txt

编辑:

如果你想硬编码路径,你可以这样做:

Scanner file = new Scanner(new File("C:\\somepath\\tobesorted.txt"));

(注意双\\)。

关于java - 文件排序和插入排序。线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11381450/

相关文章:

java - Eclipse ADT 插件 - 图形布局闪烁并耗尽内存

python - 如何在python中从文件中的字典中添加多个值

c - 努力构建可移植代码以使用 C 在任何操作系统中生成文件

python - 打开未知扩展名的现有文件

Java 勉强从量词两边匹配

java - 如何在 android 中的谷歌地图上绘制网格?

java - 如何编写一个程序来检查 sin^2(θ) + cos^2(θ) 的值?

c - 读入文件c小错误大问题

C++ 如何从文件创建 byte[] 数组(我不是说逐字节读取文件)?

ios - 如何读取录制的音频(二进制)文件以在 iOS 中上传