简体   繁体   中英

Returning to the specific place in program after handling the exception

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM