I try to catch the exception which finally works but now I need to figure out how to make it loop. ALso, how would I be able to get user input(int select) to the outside of the loop? I can try creating a new function, that might do the trick.
do {
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
select = intFind.nextInt();
} catch (InputMismatchException e) {
// Display the following text in the event of an invalid input
System.out.println("Invalid input!");
intFind.nextLine();
}
} while (select < 0);
Then
do
{
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
b = Get();
}
catch (InputMismatchException e) {
// Display the following text in the event of an invalid input
System.out.println("Invalid input!");
intFind.nextLine();
} copySel = b;
}while(select < 0);
and
static int Get()
{
Scanner intFind = new Scanner(System.in);
int select;
select = intFind.nextInt();
return select;
}
To make it loop make sure that
selectis initialized to a value less than 0. But it would be better to have a conditionselect < 2, because it makes more sense to compare at least to values.Create a method like
and call it like