Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* Reads a positive (>0) Ant number * @param msg string representing what is bein

ID: 3720908 • Letter: #

Question

* Reads a positive (>0) Ant number * @param msg string representing what is being read. The method will print a message to the user. * @return int number. e public static int readPositiveInt(String msg) /* TODO include Exception handling (InputMismatchException). *You must also verify if input is positive (0) *You must use recursion to read the value again in case it is not correct System.out.print(msg " (must be positive integer): "; int vals.nextIntO; // Ignore everything after the integer (including line break) s.nextLineQ; return val; e ae Reads a non negative (-0) double number *@param msg string representing what is being read. The method will print a message to the user. * @return double number public static double readNonNegativeDouble(String msg) /* TODO include Exception handling (InputMismatchException). You must also verify if input is *You must use recursion to read the value again in case it is not correct System.out.print(msg " (must be double 0):; double val s.nextIntQ; // Ignore everything after the double Cincluding line brea) s.nextLineQ; return val;

Explanation / Answer

//Code to copy

//ExceptionDriver.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDriver
{
   //Create an instance of Scanner class
   private static Scanner s=new Scanner(System.in);

   //start of main method
   public static void main(String[] args)
   {
       //calling readPositiveInt method
       System.out.println("User Input Number : "+readPositiveInt("Enter an integer "));
       //calling readNonNegativeDouble method
       System.out.println("User Input Double Value: "+readNonNegativeDouble("Enter a double value "));
   }

   /*
   * The method readNonNegativeDouble that takes string message
   * and prompt user to enter non negative double and continues
   * until user enters a valid positive value and return the double value.
   * */
   public static double readNonNegativeDouble(String msg)
   {
       int val = 0;
       try
       {
           System.out.println(msg+"(must be positive double)");
           val=s.nextInt();
           s.nextLine();              
           if(val<0) {
               return readPositiveInt(msg);
           }

       }
       catch (InputMismatchException e)
       {
           System.out.println("InputMismatchException");
           s.nextLine();
           return readPositiveInt(msg);
       }
       return val;  
   }

   /*
   * The method readPositiveInt that takes string message
   * and prompt user to enter positive integer and continues
   * until user enters a valid positive value and return the integer value.
   * */
   public static int readPositiveInt(String msg)
   {
       int val = 0;
       try
       {
           System.out.println(msg+"(must be positive integer)");
           val=s.nextInt();
           s.nextLine();              
           if(val<0) {
               return readPositiveInt(msg);
           }

       }
       catch (InputMismatchException e)
       {
           System.out.println("InputMismatchException");
           s.nextLine();
           return readPositiveInt(msg);
       }
       return val;  
   }
}

----------------------------------------------------------------------------------------------------

Sample Output:

Enter an integer (must be positive integer)
ten
InputMismatchException
Enter an integer (must be positive integer)
-5
Enter an integer (must be positive integer)
5
User Input Number : 5
Enter a double value (must be positive double)
ten
InputMismatchException
Enter a double value (must be positive integer)
-10.0
InputMismatchException
Enter a double value (must be positive integer)
10
User Input Double Value: 10.0