Returning to the specific place in program after handling the exception

╄→尐↘猪︶ㄣ 提交于 2021-01-29 15:14:13

问题


Good morning.

I suppose it's a very simple question for you guys...

In below code, exception NumberFormatException, can be thrown in to places, when we provide value for "a" and "b" variables. Catch block handles exceptions by starting the method again, no matter if the exception was trigged by wrong value of "a" or "b". I would like to change the code in a way that if the exception occurs while providing value for varaible "b", the method start not from the very beginning, but from the place where I'm suppose to provide value for "b" (in other words I don't want the user to go again from the start and provide value for "a" variable

Suppose I could insert two more methods handling the code where I provide the values for "a" and "b" ... but is there any other way to get the same functionality without implementing new methods ?

import java.io.*;

public class Rozdzial1{
    
    public static void Zadanie11()
    throws IOException
    {
        try {
        Double a, b, area;
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader read = new BufferedReader(input);
        System.out.println("The program calcultes area of rectangle");
        System.out.println("Provide length of first edge: ");
        a = Double.parseDouble(read.readLine());
        System.out.println("Length of the first edge is: " + a);
        System.out.println("Provide length of second edge: ");
        b = Double.parseDouble(read.readLine());    
        System.out.println("Length of the second edge is: " + b);   
        
        area = a*b;
        
        System.out.println("Area of provided rectangle is: " + area);
        }
        
        catch (NumberFormatException exception) {
            System.out.println("Provided vale should be a number, please try again\n");
            Rozdzial1.Zadanie11();
        }
    }

    public static void main(String[] args) 
    throws IOException
    {
        Rozdzial1.Zadanie11();
    }
    
}

回答1:


Reading values until valid value is entered should be extracted into a separate method, which should be called for a and b variables separately.

public static void main(String[] args) {
    Task1();
}

public static void Task1() {
    Scanner scan = new Scanner(System.in);

    System.out.println("The program calculates area of rectangle");

    double a = readValue(scan, "first edge");
    double b = readValue(scan, "second edge");
    System.out.println("Area of provided rectangle is: " + (a * b));

}

private static double readValue(Scanner scan, String name) {
    try {
        System.out.println("Provide length of " + name + ": ");
        return Double.parseDouble(scan.nextLine()); // exit from recursion with valid value
    } catch (NumberFormatException numex) {
        System.out.println("Provided value should be a number, please try again");
        return readValue(scan, name); // recursive call for invalid value
    }
}

Another option (without creating a separate method) could be to use nested loops to read inputs until valid value is provided:

public static void Task2() {
    Scanner scan = new Scanner(System.in);

    System.out.println("The program calculates area of rectangle");

    String[] names = {"first edge", "second edge"};
    double[] arr = new double[2];

    for (int i = 0; i < arr.length; i++) {
        try {
            System.out.println("Provide length of " + names[i] + ": ");
            arr[i] = Double.parseDouble(scan.nextLine());
        } catch (NumberFormatException numex) {
            System.out.println("Provided value should be a number, please try again");
            i--; // repeat the input
        }
    }

    System.out.println("Area of provided rectangle is: " + (arr[0] * arr[1]));
}



来源:https://stackoverflow.com/questions/65699872/returning-to-the-specific-place-in-program-after-handling-the-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!