error catching

This commit is contained in:
2020-06-17 16:41:54 -05:00
parent ce77a6e048
commit 0e6223e863
2 changed files with 29 additions and 15 deletions

View File

@@ -21,6 +21,7 @@ public class AlgorithmTester{
System.out.println("Please select an option:"); System.out.println("Please select an option:");
System.out.println("1) Make a file of random numbers"); System.out.println("1) Make a file of random numbers");
System.out.println("2) Sort a file of random numbers"); System.out.println("2) Sort a file of random numbers");
System.out.println("3) Sort a file of words");
System.out.println("0) Exit"); System.out.println("0) Exit");
try{ try{
input = sc.nextInt(); input = sc.nextInt();
@@ -29,6 +30,7 @@ public class AlgorithmTester{
switch(input){ switch(input){
case 1 -> makeFile(); case 1 -> makeFile();
case 2 -> sortFile(); case 2 -> sortFile();
case 3 -> sortWordFile();
case 0 -> quit=true; case 0 -> quit=true;
default -> System.out.println("Invalid input"); default -> System.out.println("Invalid input");
} }
@@ -88,6 +90,7 @@ public class AlgorithmTester{
boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length]; boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length];
System.out.print("\nPlease enter file name: "); System.out.print("\nPlease enter file name: ");
filename = sc.next(); filename = sc.next();
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
while(!ready){ while(!ready){
try{ try{
System.out.println("\nEnter the number of an algorithm you wish to use."); System.out.println("\nEnter the number of an algorithm you wish to use.");
@@ -139,7 +142,6 @@ public class AlgorithmTester{
boolean quickTWP=sortingAlgoChoices[6]; boolean quickTWP=sortingAlgoChoices[6];
boolean merge=sortingAlgoChoices[7]; boolean merge=sortingAlgoChoices[7];
ArrayList<SortResult> results = new ArrayList<SortResult>(); ArrayList<SortResult> results = new ArrayList<SortResult>();
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
if(bubble){ if(bubble){
BubbleSorter bubSorter = new BubbleSorter(reader); BubbleSorter bubSorter = new BubbleSorter(reader);
results.add(bubSorter.measuredSort()); results.add(bubSorter.measuredSort());
@@ -173,6 +175,7 @@ public class AlgorithmTester{
results.add(mSorter.measuredSort()); results.add(mSorter.measuredSort());
} }
for(SortResult e: results){ for(SortResult e: results){
if(e!=null){
if(print){ if(print){
for(int i: e.getSortedArray()){ for(int i: e.getSortedArray()){
System.out.print(i+" "); System.out.print(i+" ");
@@ -184,6 +187,11 @@ public class AlgorithmTester{
System.out.println(e.getWritesUsed()+" write operations"); System.out.println(e.getWritesUsed()+" write operations");
System.out.println(e.getTimeUsed()+" milliseconds"); System.out.println(e.getTimeUsed()+" milliseconds");
} }
}
} }
private static void sortWordFile(){
//String filename;
}
} }

View File

@@ -68,6 +68,7 @@ abstract class Sorter {
* @return returns a SortResult object with the name of the sorting algorithm used, the sorted array of numbers, the number of comparisons used to sort, and the number of writes to arrays used to sort * @return returns a SortResult object with the name of the sorting algorithm used, the sorted array of numbers, the number of comparisons used to sort, and the number of writes to arrays used to sort
*/ */
public SortResult measuredSort(){ public SortResult measuredSort(){
if(numbers.length!=0){
Instant start = Instant.now(); Instant start = Instant.now();
this.sort(); this.sort();
Instant end = Instant.now(); Instant end = Instant.now();
@@ -75,6 +76,11 @@ abstract class Sorter {
SortResult output = new SortResult(sortType, numbers, comparisonsUsed, writesUsed, time); SortResult output = new SortResult(sortType, numbers, comparisonsUsed, writesUsed, time);
return output; return output;
} }
else{
System.out.println("Nothing to sort using "+sortType);
return null;
}
}
} }