I am working on a program that uses Radix Sort that reads in words from a file and sorts in ascending order. In order to do this, we must check that all the words in the file are the same length and there is nothing in the file but letter (no symbols, etc). I have the sort working, but I cannot catch whether the strings are the same length or if there are any symbols. Any help on how to write these exception catches would be appreciated.
This is my radix Sort class. There are two comments on where I need to catch the exceptions.
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]+");
}
}
This is the driver that was provided for me in order to create the Radix Sort 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);
}
}
This is the Junit Test class that was provided for us in order to catch the exceptions
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");
}
}
Finally, this is the exception class that was created so we knew what exception to call
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);
}
}
Use this method to throw exceptions:
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");
}
}
}