[英]Try-Catch does not rerun when there is code after the try-catch block
我正在尝试在do-while循环中运行多个try-catch块来帮助捕获诸如整数和双输入的InputMissMatch之类的错误,但是,我仍然需要能够在相同的do-while循环中从输入字符串控制台界面。
是否有可能在捕获异常后使try-catch块重新运行,而不是移至下一行代码,如果可以,怎么办?
这是我的代码:
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
您可以从catch语句中调出方法,也可以添加一个while循环来检查有效输入。
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()
这样的事情可能会起作用:
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()
您当然可以在循环中提示输入。 问题是...您没有使用循环:
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();
}
}
如果要循环执行此逻辑,则将其包装在循环中。 在这种情况下,终止条件将是输入是否成功。 因此,您可以使用一个简单的布尔标志来跟踪它。 也许是这样的:
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");
}
}
}
尝试在catch块内尝试执行continue label语句以尝试block 。 它将反复尝试获取输入,直到没有异常被捕获为止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.