I'm trying to run multiple try-catch blocks in a do-while loop to help catch errors such as InputMissMatch for integer and double inputs , however , I still need to be able to input strings in the same do-while loop from the console interface.
Is it possible to get the try-catch blocks to rerun after catching an exception, instead of moving onto the next line of code and if so, how?
This is my code:
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class EmployeeBenefits {
Map <Integer, CaptureDetails> map = new HashMap<> ();
int number;
String name;
String surname;
double grossSalary;
int employeeNumber = 1;
int numberOfEmployees = 0;
public void captureDetails() {
boolean codeIsRunning = true;
Scanner input = new Scanner (System.in);
do {
inputNumberOfEmployees ();
if (numberOfEmployees != 0) {
for (int enterDetails = 0; enterDetails < numberOfEmployees; enterDetails++) {
inputEmployeeNumber ();
inputEmployeeNames ();
inputGrossSalary ();
map.put(employeeNumber, new CaptureDetails (number, name, surname, grossSalary));
employeeNumber++;
}
}
codeIsRunning = false;
} while (codeIsRunning); // end of do-while loop
} // end of captureDetails () method
public void inputNumberOfEmployees () {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter the number of employees to input: ");
numberOfEmployees = input.nextInt();
}
catch (InputMismatchException e) {
System.out.println ("\nYou must enter an integer value. Please try agin\n");
input.nextLine();
}
} // end of inputNumberOfEmployees()
public void inputEmployeeNumber () {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter Number: ");
number = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("\nYou must enter an integer value. Please try agin\n");
input.nextLine ();
}
} // end of inputEmployeeNumber()
public void inputGrossSalary () {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter Gross Salary: ");
grossSalary = input.nextDouble();
}
catch (InputMismatchException e) {
System.out.println ("\nEnter employees salary. Please try agin\n");
input.nextLine ();
}
} // inputGrossSalary ()
public void inputEmployeeNames () {
Scanner input = new Scanner (System.in);
System.out.print ("Enter Name: ");
name = input.nextLine();
System.out.print ("Enter Surname: ");
surname = input.nextLine();
} // end of inputEmployeeNames
public static void main(String[] args) {
EmployeeBenefits eb = new EmployeeBenefits ();
boolean programIsRunning = true;
Scanner input = new Scanner (System.in);
while (programIsRunning) {
System.out.println ("1. Enter Employee details");
System.out.println ("2. Set New Salary");
System.out.println ("3. Print Employee Details");
System.out.println ("4. Exit");
switch (input.nextInt()) {
case 1:
eb.captureDetails ();
break;
case 2:
break;
case 3:
break;
case 4:
programIsRunning = false;
break;
default :
System.out.println ("Enter a value between from 1 to 4\n");
} // end of switch-case (input.nextInt())
} // end of while (programIsRunning) loop
} // end of main method
} // end of EmployeeBenefits class
class CaptureDetails {
int number;
String name, surname;
double grossSalary;
public CaptureDetails (int number, String name, String surname, double grossSalary) {
this.number = number;
this.name = name;
this.surname = surname;
this.grossSalary = grossSalary;
}
} // end of CaptureDetails class
You can recall your method from the catch statement or you can add a while loop that checks for valid input.
public void inputNumberOfEmployees () {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter the number of employees to input: ");
numberOfEmployees = input.nextInt();
}
catch (InputMismatchException e) {
System.out.println ("\nYou must enter an integer value. Please try agin\n");
inputNumberOfEmployees(); // This is the Change
}
} // end of inputNumberOfEmployees()
Something like this might work:
public void inputEmployeeNumber () {
Scanner input = new Scanner (System.in);
boolean isValid = false;
while (!isValid)
{
try {
System.out.print ("Enter Number: ");
number = input.nextInt();
isValid = true;
} catch (InputMismatchException e) {
System.out.println("\nYou must enter an integer value. Please try agin\n");
input.nextLine ();
}
}
} // end of inputEmployeeNumber()
You certainly can prompt for input in a loop. The problem is... You're not using a loop:
public void inputNumberOfEmployees () {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter the number of employees to input: ");
numberOfEmployees = input.nextInt();
}
catch (InputMismatchException e) {
System.out.println ("\nYou must enter an integer value. Please try agin\n");
input.nextLine();
}
}
If you want to perform this logic in a loop, then wrap it in a loop. In this case the terminating condition would be whether or not the input was successful. So you can track that with a simple boolean flag. Perhaps something like this:
public void inputNumberOfEmployees () {
Scanner input = new Scanner (System.in);
boolean isInvalid = true;
while (isInvalid) {
try {
System.out.print ("Enter the number of employees to input: ");
numberOfEmployees = input.nextInt();
isInvalid = false;
}
catch (InputMismatchException e) {
System.out.println ("\nYou must enter an integer value.\n");
}
}
}
Try a continue label statement inside catch block to try block . It will repeatedly try to get the input untill no exception is caught
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.