简体   繁体   中英

while true try catch nested

I'm new to Java. I want the code to repeat at the point where the user input is the wrong type rather than starting from the beginning. When at "enter b: " or "Enter c: " it goes back to the beginning "Enter a: ". I want it to repeat only, where the user input is a, b, c. Thanks in advance.

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
boolean itsANumber = true;
while (itsANumber)
{
    System.out.print("Enter a: ");

    try
    {
    a = Double.parseDouble(sc.nextLine());

    System.out.print("Enter b: ");
    try
    {
        b = Double.parseDouble(sc.nextLine());

        System.out.print("Enter c: ");
        try
        {
        c = Double.parseDouble(sc.nextLine());
        if (a == 0)
        {
            aZero();
        } else

        {
            aNotZero();
        }

        } catch (NumberFormatException nfe) 
        {
        System.out
            .println("That's not a number, please try again!");
        }

    } catch (NumberFormatException nfe) 
    {
        System.out
            .println("That's not a number, please try again!");
    }

    } catch (NumberFormatException nfe) 
    {
    System.out.println("That's not a number, please try again!");
    }
}

}

Introduce a method that asks for a number and call it three times. Inside the method you'll have the while loop with try-catch .

public static void main(String... args) {
   Scanner sc = new Scanner(System.in);
   double
     a = askForDouble(sc, "a"),
     b = askForDouble(sc, "b"),
     c = askForDouble(sc, "c");
}
static double askForDouble(Scanner sc, String varName) {
   for (;/*ever*/;) {
     System.out.format("Enter %s: ", varName);
     System.out.flush();
     try {
       return Double.parseDouble(sc.nextLine());
     } catch (NumberFormatExcetpion() {
       System.out.println("That's not a number, please try again!");
     }
   }
}

You can try by simple null checking if you want as below, this is raw code and may require change

Double a = null;
Double b = null;
Double c = null;
Scanner sc = new Scanner(System.in);
while(true){

    if(a != null){              
        System.out.println("Enter a :");
        a = readFromInStream(sc, "a");
            if(a == null) continue;
    }
    if(b != null){              
        System.out.println("Enter b :");
        b = readFromInStream(sc, "b");
            if(b == null) continue;
    }
    if(c != null){              
        System.out.println("Enter c :");
        c = readFromInStream(sc, "c");
            if(c == null) continue;
    }
    if(a != null && b != null && c != null){
        break;
    }
}

private Double readFromInStream(Scanner sc, String varStr){
    Double temp = null;
    try
    {
        temp = Double.parseDouble(sc.nextLine());
    }catch(NumberFormatException e){
        System.out.println("Invalid value for :"+varStr);
    }
    return temp;
}

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