简体   繁体   English

为什么我在线程“main”java.lang.IllegalStateException 中收到此异常:方法未实现

[英]why i am getting this Exception in thread "main" java.lang.IllegalStateException: Method not implemented

Why I am getting this exception while run the main class?为什么在运行主类时出现此异常?

The data returned by Sort.sort is sorted.
Sort.sort took 0.5760083 seconds to read and sort the data.
Exception in thread "main" java.lang.IllegalStateException: Method not implemented
    at Sort.threadedSort(Sort.java:21)
    at SortTest.main(SortTest.java:41)
C:\Users\DELL\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\DELL\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)

This is my MergeSort class这是我的MergeSort

public class MergeSort {

    // The mergeSort method returns a sorted copy of the
    // String objects contained in the String array data.
    /**
     * Sorts the String objects using the merge sort algorithm.
     * 
     * @param data the String objects to be sorted
     * @return the String objects sorted in ascending order
     */
    public static String[] mergeSort(String[] data) {
        if (data.length > 1) {
            String[] left = new String[data.length / 2];
            String[] right = new String[data.length - left.length];
            System.arraycopy(data, 0, left, 0, left.length);
            System.arraycopy(data, left.length, right, 0, right.length);
            left = mergeSort(left);
            right = mergeSort(right);
            return merge(left, right);
        }
        else {
            return data;
        }
    }

    /**
     * The merge method accepts two String arrays that are assumed
     * to be sorted in ascending order. The method will return a
     * sorted array of String objects containing all String objects
     * from the two input collections.
     * 
     * @param left a sorted collection of String objects
     * @param right a sorted collection of String objects
     * @return a sorted collection of String objects
     */
    public static String[] merge(String[] left, String[] right) {
        String[] data = new String[left.length + right.length];
        int lIndex = 0;
        int rIndex = 0;
        for (int i=0; i<data.length; i++) {
            if (lIndex == left.length) {
                data[i] = right[rIndex];
                rIndex++;
            }
            else if (rIndex == right.length) {
                data[i] = left[lIndex];
                lIndex++;
            }
            else if (left[lIndex].compareTo(right[rIndex]) < 0) {
                data[i] = left[lIndex];
                lIndex++;
            }
            else {
                data[i] = right[rIndex];
                rIndex++;
            }
        }
        return data;
    }
}

This is my Sort class.这是我的Sort课。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Sort {

    /**
     * You are to implement this method. The method should invoke one or
     * more threads to read and sort the data from the collection of Files.
     * The method should return a sorted list of all of the String data 
     * contained in the files.
     * 
     * @param files
     * @return
     * @throws IOException 
     */
    public static String[] threadedSort(File[] files) throws IOException {
        throw new java.lang.IllegalStateException("Method not implemented");
    }

    /**
     * Given an array of files, this method will return a sorted 
     * list of the String data contained in each of the files.
     * 
     * @param files the files to be read
     * @return the sorted data
     * @throws IOException thrown if any errors occur reading the file
     */
    public static String[] sort(File[] files) throws IOException {
        String[] sortedData = new String[0];
        for (File file : files) {
            String[] data = getData(file);
            data = MergeSort.mergeSort(data);
            sortedData = MergeSort.merge(sortedData, data);
        }
        return sortedData;
    }

    /**
     * This method will read in the string data from the specified 
     * file and return the data as an array of String objects.
     * 
     * @param file the file containing the String data
     * @return String array containing the String data
     * @throws IOException thrown if any errors occur reading the file
     */
    private static String[] getData(File file) throws IOException {
        ArrayList<String> data = new ArrayList<String>();
        BufferedReader in = new BufferedReader(new FileReader(file));
        // Read the data from the file until the end of file is reached
        while (true) {
            String line = in.readLine();
            if (line == null) {
                // the end of file was reached
                break;
            }
            else {
                data.add(line);
            }
        }
        //Close the input stream and return the data
        in.close();
        return data.toArray(new String[0]);
    }
}

This is my SortTest class:这是我的SortTest课程:

import java.io.File;
import java.io.IOException;

/**
 * The class SortTest is used to test the threaded and non-threaded
 * sort methods. This program will call each method to sort the data
 * contained in the four test files. This program will then test the
 * results to ensure that the results are sorted in ascending order.
 * 
 * Simply run this program to verify that you have correctly implemented
 * the threaded sort method. The program will not verify if your sort
 * uses threads, but will verify if your implementation correctly 
 * sorted the data contained in the four files.
 * 
 * There should be no reason to make modifications to this class.
 */
public class SortTest {

    public static void main(String[] args) throws IOException {
        File[] files = {new File("enable1.txt"),
                        new File("enable2k.txt"),
                        new File("lower.txt"),
                        new File("mixed.txt")};
        // Run Sort.sort on the files
        long startTime = System.nanoTime();
        String[] sortedData = Sort.sort(files);
        long stopTime = System.nanoTime();
        double elapsedTime = (stopTime - startTime) / 1000000000.0;

        // Test to ensure the data is sorted
        for (int i=0; i<sortedData.length-1; i++) {
            if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
                System.out.println("The data returned by Sort.sort is not sorted.");
                throw new java.lang.IllegalStateException("The data returned by Sort.sort is not sorted");
            }
        }
        System.out.println("The data returned by Sort.sort is sorted.");
        System.out.println("Sort.sort took " + elapsedTime + " seconds to read and sort the data.");

        // Run Sort.threadedSort on the files and test to ensure the data is sorted
        startTime = System.nanoTime();
        String[] threadSortedData = Sort.threadedSort(files);
        stopTime = System.nanoTime();
        double threadedElapsedTime = (stopTime - startTime)/ 1000000000.0;

        // Test to ensure the data is sorted
        if (sortedData.length != threadSortedData.length) {
            System.out.println("The data return by Sort.threadedSort is missing data");
            throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
        }
        for (int i=0; i<threadSortedData.length-1; i++) {
            if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {
                System.out.println("The data return by Sort.threadedSort is not sorted");
                throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
            }
        }
        System.out.println("The data returned by Sort.threadedSort is sorted.");
        System.out.println("Sort.threadedSort took " + threadedElapsedTime + " seconds to read and sort the data.");
    }
}

From your own post:从你自己的帖子:

public class Sort {

   /**
          * You are to implement this method. The method should invoke one or
       * more threads to read and sort the data from the collection of Files.
         * The method should return a sorted list of all of the String data 
       * contained in the files.
            * 
       * @param files
        * @return
        * @throws IOException 
               */
         public static String[] threadedSort(File[] files) throws IOException {
          throw new java.lang.IllegalStateException("Method not implemented");
           }

That's the exception that you're seeing, and it's there because you didn't do what the comment told you to do yet.这是您看到的例外情况,因为您还没有按照评论告诉您的操作去做。

暂无
暂无

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

相关问题 为什么我在 JavaFX 上收到 java.lang.IllegalStateException “Not on FX application thread”? - Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX? 为什么我在Google Dataflow上收到java.lang.IllegalStateException? - Why am I getting java.lang.IllegalStateException on Google Dataflow? 为什么在递归java方法中出现“线程“ main”中的异常“ main” java.lang.StackOverflowError”? - Why Am I getting “Exception in thread ”main“ java.lang.StackOverflowError” in recursive java method? 使用Selenium的线程“main”java.lang.IllegalStateException中的异常 - Exception in thread “main” java.lang.IllegalStateException for using Selenium 线程“main”中的异常 java.lang.IllegalStateException:已连接 - Exception in thread “main” java.lang.IllegalStateException: Already connected 线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件是一个目录 - Exception in thread “main” java.lang.IllegalStateException: The driver executable is a directory 在线程“主要” java.lang.IllegalStateException中获取错误接收 - Getting error xception in thread “main” java.lang.IllegalStateException 如何修复线程“main”中的异常 java.lang.IllegalStateException - How do I fix Exception in thread "main" java.lang.IllegalStateException Android java.lang.IllegalStateException,不在主线程上 - Android java.lang.IllegalStateException, not on the main thread java.lang.IllegalStateException:不在主线程上 - java.lang.IllegalStateException: Not on the main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM