当try-catch块之后有代码时,Try-Catch不会重新运行

时间:2017-09-22 16:15:43

标签: java netbeans

我试图在do-while循环中运行多个try-catch块以帮助捕获错误,例如InputMissMatch用于整数和双输入,但是,我仍然需要能够在同一个输入中输入字符串 - 从控制台界面循环。

是否有可能在捕获异常后重新运行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

4 个答案:

答案 0 :(得分:1)

您可以从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()

答案 1 :(得分:1)

这样的事可能有用:

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()

答案 2 :(得分:1)

你当然可以在循环中提示输入。问题是......你没有使用循环:

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");
        }

    }
}

答案 3 :(得分:-2)

catch block 中尝试继续 标签语句,以尝试阻止。它会反复尝试获取输入,直到没有异常被捕获