[英]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.