In my Exception Throwing assignment we were given a code to edit, and told to make a loop within the numberFromUser method. A Scanner should input a new value until its value equals a integer that is not zero, in which case it would solve 7 % x where x is the value imputed.
My problem is my loop isn't working, I was able to get it to work in the main method, however in the instructions they specifically say they want the loop in the numberFromUser method.
This is my code, and everything works except for the do, while loop.
package exception.handling; import java.util.InputMismatchException; import java.util.Scanner; public class LabExceptionHandilng { public static void main(String[] args) { try { int digit = numberFromUser(); int result = sevenModulusN(digit); System.out.printf("7 %% %d = %d", digit, result); } catch(IllegalArgumentException e) { System.out.println("A problem occurred: 7 % 0 is an invalid operation!"); } catch(InputMismatchException e) { System.out.println("A problem Occured: The number entered needs to be a whole number."); } } private static int numberFromUser() { Scanner input = new Scanner(System.in); boolean done = false; Integer value = 1; do { System.out.print("number: "); value = input.nextInt(); if(value instanceof Integer && value != 0) done = true; if(value == 0) throw new IllegalArgumentException("7 % 0 is a bad operation!"); }while(!done); return value; } private static int sevenModulusN(int number) { return 7 % number; } }
This is the code given to us prior to edit.
import java.util.Scanner; public class LabExceptionHandilng { public static void main(String[] args) { int digit = numberFromUser(); int result = sevenModulusN(digit); System.out.printf("7 %% %.1f = %.1f", digit, result); } private static int numberFromUser() { Scanner input = new Scanner(System.in); System.out.print("number: "); return input.nextInt(); } private static int sevenModulusN(int number) { return 7 % number; } }
I have a feeling, I'm overlooking something but I cant figure it out. Any help would be much appreciated.