基数排序调用预期异常

Radix Sort- Calling Expected exceptions

我正在开发一个使用基数排序的程序,该程序从文件中读取单词并按升序排序。为此,我们必须检查文件中所有单词的长度是否相同,并且文件中只有字母(没有符号等)。我有排序工作,但我无法判断字符串是否相同长度或者是否有任何符号。任何有关如何编写这些异常捕获的帮助将不胜感激。

这是我的基数排序class。关于我需要在哪里捕获异常有两条评论。

import java.util.ArrayList;
import java.util.Queue;

public class RadixSort implements RadixSortADT {


    private ArrayList<String> string;
    private ArrayList<LinkedQueue<String>> queues;
    private String result;


    public RadixSort(ArrayList<String> words) {
        string = new ArrayList<>();
        queues = new ArrayList<>();
        initializeList();
        initializeWords(words);
    }

    public void initializeList() {
        for(int i = 0; i < 26; i++){
            queues.add(new LinkedQueue<String>());
        }

    }

    public void initializeWords(ArrayList<String> w) {
        for(int i = 0; i < w.size(); i++){
            string.add(w.get(i).toLowerCase());
        }

    }

    public void sort() {

        if(string.isEmpty()){
            throw new EmptyCollectionException("File");
        }
//Exception needed for string length comparisons
//Exception needed for nonletters in string

        for(int j = string.get(0).length()-1; j>= 0; j--){
            for(int k = 0; k <string.size(); k++){
                char c = string.get(k).charAt(j);
                queues.get(c-97).enqueue(string.get(k));
            }
            int i = 0;
            for(int n = 0; n <queues.size(); n++){
                while(!queues.get(n).isEmpty()){
                    string.set(i++, queues.get(n).dequeue());
                }
            }
        }
    }

    public String toString(){
        String result = "";
        for(String words: string){
            result += words + " ";
        }
        return result;
    }

    public boolean isAlpha(ArrayList<String> string2) {
        return string2.contains("[a-zA-Z]+");
    }

}

这是为创建基数排序而提供给我的驱动程序 class

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class RadixSortDriver {

    public static void main(String[] args) throws FileNotFoundException{
        int i = 0;
        ArrayList<String> words = new ArrayList<>();
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the name of the file to import words");
        String filename = scan.nextLine();
        //String filename = "four.txt";
        Scanner inFile = new Scanner(new File(filename));
        while(inFile.hasNext()) {
            words.add(inFile.nextLine());
        }
        RadixSort r = new RadixSort(words);
        System.out.println("Unsorted List:\n" + r);
        r.sort();
        System.out.println("\n\nSorted List:\n" + r);
    }

}

这是为我们提供的用于捕获异常的 Junit 测试class

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import org.junit.Test;

public class RadixSortTest {

    @Test
    public void testsort1() throws FileNotFoundException {
        ArrayList<String> words = new ArrayList<>();
        Scanner inFile = new Scanner(new File("four.txt"));

        while(inFile.hasNext()) {
            words.add(inFile.nextLine());
        }
        RadixSort evaluator = new RadixSort(words);
        evaluator.sort();
        assertEquals("atom barn crew goat home kite love rain soap xray yarn ", evaluator.toString());
    }

    @Test
    public void testsort5() throws FileNotFoundException {
        ArrayList<String> words = new ArrayList<>();
        Scanner inFile = new Scanner(new File("fourUpper.txt"));

        while(inFile.hasNext()) {
            words.add(inFile.nextLine());
        }
        RadixSort evaluator = new RadixSort(words);
        evaluator.sort();
        assertEquals("atom barn crew goat home kite love rain soap xray yarn ", evaluator.toString());
    }   

    @Test(expected=EmptyCollectionException.class)
    public void testsort7() throws FileNotFoundException {
        ArrayList<String> words = new ArrayList<>();
        Scanner inFile = new Scanner(new File("empty.txt"));

        while(inFile.hasNext()) {
            words.add(inFile.nextLine());
        }
        RadixSort evaluator = new RadixSort(words);
        evaluator.sort();
        fail("Empty list Exception not caught");


    }

    @Test(expected=InvalidRadixSortException.class)
    public void testsort10() throws FileNotFoundException {
        ArrayList<String> words = new ArrayList<>();
        Scanner inFile = new Scanner(new File("error3.txt"));

        while(inFile.hasNext()) {
            words.add(inFile.nextLine());
        }
        RadixSort evaluator = new RadixSort(words);
        evaluator.sort();
        fail("Invalid Length Exception not caught");


    }

}

最后,这是创建的异常 class,因此我们知道要调用什么异常

public class InvalidRadixSortException extends RuntimeException
{
    /**
     * Sets up this exception with an appropriate message.
     * @param collection the name of the collection
     */
    public InvalidRadixSortException(String error)
    {
        super("The list of words contained an invalid " + error);
    }

}

使用此方法抛出异常:

    private void validateWords() {
        int length = string.get(0).length();

        for (int j = 0; j < string.size(); j++) {

            if (string.get(j).length() != length) {
                throw new InvalidRadixSortException("Invalid String Length");
            }

            if (!string.get(j).matches("[a-zA-Z]+")) {
                throw new InvalidRadixSortException("Contains Non-Letters");
            }
        }
    }