I have chosen a do-while loop mechanism for my first try catch Java program, but it's kind of a mess:
package sam; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Scanner; public class Test { public static void main(String[] args){ System.out.println("Lets play with some try catch statements."); Scanner input = new Scanner(System.in); int x=1, num1=1, num2=1; do{ try{ System.out.println("Enter the first (dividend) int: "); num1 = input.nextInt(); int parsedNum1 = Integer.parseInt(num1); System.out.println("Enter the second (divisor) int: "); num2 = input.nextInt(); int parsedNum2 = Integer.parseInt(num2); int quotient = num1 / num2;//dividend / divisor System.out.println("Our quotient is: " + quotient); x = 2;//when the user enters proper input, change x, so that the while condition below can terminate our do while loop. } catch(NumberFormatException e){ //check if num1 is an integer //check if num2 is an integer //statement to catch a NumberFormatException? System.out.println("Error. Invalid dividend or divisor. Please try again."); continue; } }while(x==1); }//end of main }//end of class Test
Lines 17 and 20 keep throwing the same error, "The method parseInt(String) in the type Integer is not applicable for the arguments (int)".
I don't understand how the "e" parameter works in line 27's catch statement. Does it have to be "e" for a special reason? Would it still work if I used "fred" for the parameter instead?
I'm unsure how to proceed making exception handlers. Am I just supposed to use a bunch of if statements? Do I need to use the continue statement in a do-while loop?