advance scanner past bad input on error

This commit is contained in:
2020-06-11 11:05:47 -05:00
parent c67a2f25d5
commit 7f59fadc42

View File

@@ -23,34 +23,35 @@ public class AlgorithmTester{
System.out.println("2) Sort a file of random numbers"); System.out.println("2) Sort a file of random numbers");
System.out.println("0) Exit"); System.out.println("0) Exit");
try{ try{
input = sc.nextInt(); input = sc.nextInt();
//Using the new switch syntax introduced in JDK 13 //Using the new switch syntax introduced in JDK 13
//this is a switch expression (lambda expression) rather than a switch statement //this is a switch expression (lambda expression) rather than a switch statement
switch(input){ switch(input){
case 1 -> makeFile(); case 1 -> makeFile();
case 2 -> sortFile(); case 2 -> sortFile();
case 0 -> quit=true; case 0 -> quit=true;
default -> System.out.println("Invalid input"); default -> System.out.println("Invalid input");
} }
/* /*
non-lambda expresstion version looks like this non-lambda expresstion version looks like this
switch(input){ switch(input){
case 1: case 1:
makeFile(); makeFile();
break; break;
case 2: case 2:
sortFiler(); sortFiler();
break; break;
case 0: case 0:
quit=true; quit=true;
break; break;
default: default:
System.out.println("Invalid input"); System.out.println("Invalid input");
break; break;
} }
*/ */
} catch(InputMismatchException ex){ } catch(InputMismatchException ex){
System.out.println("Invalid input"); System.out.println("Invalid input exception");
sc.next();
} }
} }
sc.close(); sc.close();
@@ -73,7 +74,8 @@ public class AlgorithmTester{
var randomMaker = new RandomNumberFileMaker(filename, count, min, max); var randomMaker = new RandomNumberFileMaker(filename, count, min, max);
randomMaker.writeFile(); randomMaker.writeFile();
} catch(InputMismatchException ex){ } catch(InputMismatchException ex){
System.out.println("Invalid input"); System.out.println("Invalid input.");
sc.next();
} }
} }
@@ -111,6 +113,7 @@ public class AlgorithmTester{
} }
}catch(InputMismatchException ex){ }catch(InputMismatchException ex){
System.out.println("Invalid input"); System.out.println("Invalid input");
sc.next();
} }
} }
try{ try{
@@ -125,6 +128,7 @@ public class AlgorithmTester{
} }
}catch(InputMismatchException ex){ }catch(InputMismatchException ex){
System.out.println("Invalid input, defaulting to no"); System.out.println("Invalid input, defaulting to no");
sc.next();
} }
boolean bubble=sortingAlgoChoices[0]; boolean bubble=sortingAlgoChoices[0];
boolean selection=sortingAlgoChoices[1]; boolean selection=sortingAlgoChoices[1];