简体   繁体   English

冒泡排序作业有误

[英]Error with bubble sort homework

Basically, I have homework that requires me to take an array of strings and a string search term, and return the position of the search term in the array (or -1 if not found)and take an array of strings and bubble sort it. 基本上,我的作业需要我获取一个字符串数组和一个字符串搜索项,并返回搜索项在数组中的位置(如果未找到则返回-1)并获取一个字符串数组并对其进行冒泡排序。 I am getting an error with what seems to only be the bubble sort and I have no idea why. 我在似乎只是气泡排序的问题上遇到了错误,我也不知道为什么。

The error message I'm getting is: 我收到的错误消息是:

Exception in thread "main" java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1193)
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1186)
at java.lang.String.compareToIgnoreCase(String.java:1239)
at mmtextreader.MMTextReader.bubbleSort(MMTextReader.java:100)
at mmtextreader.MMTextReader.main(MMTextReader.java:47)

Code: 码:

public class TextReader {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here

        String[] Array;

        Array = wordPut("beowulf.txt");

        Scanner reader = new Scanner(System.in);

        System.out.println("Input word: ");

        String term = reader.next();

        int position = pFind(Array, term);

        if(position == -1) {
            System.out.println("no word found");
        } else {
            System.out.println(term + " is found at " + (position+1));
        }

        bubbleSort(Array);
    }

    public static String[] wordPut(String s) throws FileNotFoundException {
        String[] Array = new String[50000];

        Scanner reader = new Scanner(new BufferedReader(new FileReader(s))).useDelimiter("[^\\p{Alpha}']+");

        int wordcount = 0;
        while (reader.hasNext()) {
            Array[wordcount] = reader.next();
            wordcount++;
        }
        for (int i = 0; i < Array.length; i++) {
            if (Array[i] != null) {
                System.out.println(Array[i]);
            }
        }

        return Array;
    }

    public static String[] bubbleSort(String[] a) {
        int lElement = a.length - 1;
        boolean t = true;
        for (int i = 0; i <= a.length - 1; i++) {

            boolean swap = false;

            for (int j = 0; j < lElement; j++) {
                if (a[j].compareTo(a[j + 1]) > 0) {
                    String store = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = store;
                    swap = true;
                }
            }
            lElement--;
            if (swap == false) {
                break;
            }
        }
        for(int n = 0; n < a.length; n++) {
            System.out.println(a[n]);
        }
        return a;
    }

    public static int pFind(String[] a, String trm) {
        int pos = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i].compareToIgnoreCase(trm) == 0) {
                pos = i;
                break;
            }
        }
        return pos;
    }

}

OK! 好! I checked your code and the only problem you have is you inicialize your array to have size of 5000 which is the problem in wordPut() method 我检查了您的代码,唯一的问题是将数组初始化为5000,这是wordPut()方法中的问题

  String[] Array = new String[50000];

I tried on my computer your code, works fine, also the buble sort is going well, except that declaration of array, try to find out the size of array and then declare! 我在我的计算机上尝试了您的代码,工作正常,异常排序也进行得很顺利,除了声明数组,尝试找出数组的大小然后声明!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM