Question
PROJECT: Exception Handling Given a program (Test.java in HW8.zip) that gets two numbers from user input and then carries out a division, revise it to
PROJECT: Exception Handling
Given a program (Test.java in HW8.zip) that gets two numbers from user input and then carries out a division, revise it to handle exceptional inputs. The given program does those:
Prompt 1
Input 1
Prompt 2
Input 2
Calculation
For the purpose of this project, input 1 should be a double value and input 2 should be a non-zero int value. Upon an invalid input, your program should allow a user to re-enter a piece of data. This would require a loop to validate each input and allow for additional data entry. We need a loop instead of an if-else as you can't assume a second input would be valid.
You will complete this exercise using two approaches:
a. use try-catch.
b. Use defensive coding: check for invalid data ahead of time.
Save each version in a different source code file.
Version A: Try-Catch
Because invalid data must be removed from an input stream object before additional input could happen, you need to read in each number as a string and then parse the data in the string.
Follow this pseudo code for the try-catch version:
Do // loop if wrong type of input
isValid true
Try
Prompt 1
Input 1: read a string and parse double
Catch XxxException
isValid false
display err msg
End Try-Catch
While !isValid
Do // loop if wrong type or zero
isValid true
Try
Prompt 2
Input 2: read a string and parse int
If Zero
isValid false
display err msg
End If
Catch XxxxException
isValid false
display err msg
End Try-Catch
While !isValid
Calculation
For full credit, you must use proper exception classes instead of a generic one. The try-catch version may work like this:
Enter dividend: 4.2.3
Invalid input: multiple points
Enter dividend: 4/5
Invalid input: For input string: "4/5"
Enter dividend: 4.5
Enter divisor: 1.5
Invalid input: (divisor) For input string: "1.5"
Enter divisor: 0
Input error: divisor can't be zero. Please input again.
Enter divisor: 3
4.5 / 3 = 1.5
User input is provided right after each "Enter xxx: ". The message for 0 input is a fixed message. The message for other invalid input is generated with:
System.out.println("Invalid input: " +yourExceptionObj.getMessage());
Or
System.out.println("Invalid input: (divisor) " +yourExceptionObj.getMessage()); // for input 2
Version B: Defensive Coding
You need to use the hasNextXXX methods of Scanner class for the defensive coding version.
public boolean hasNextDouble()
public boolean hasNextInt()
A hasNextXXX method peeks (does NOT read) into user input to decide if the next string to read contains a valid XXX. Use Java API 8 documentation if needed. The following snippet of code illustrates how hasNextXXX may be used:
if (stdIn.hasNextDouble()) { //check before read
dividend = stdIn.nextDouble();
}
else {// no valid input
stdIn.next(); // read string and discard
System.out.println("Invalid dividend");
isValid = false;
}
Follow this pseudo code for defensive coding version:
Do // first num
isValid true
Prompt 1
If has double input
Input 1: nextDouble()
Else
extract input: next()
display err msg
isValid false
End If
While !isValid
Do // second num
isValid true
Prompt 2
If has int input
Input 2: nextInt()
If zero
display err msg
isValid false
End If
Else
extract input: next()
display err msg
isValid false
End If
While !isValid
calculation
This version may work like this:
Enter dividend: 4.2.3
Invalid dividend
Enter dividend: 4/5
Invalid dividend
Enter dividend: 4.5
Enter divisor: 1.5
Invalid divisor
Enter divisor: 0
Input error: divisor can't be zero. Please input again.
Enter divisor: 3
4.5 / 3 = 1.5
Overall comment your program appropriately. Pay attention to standard stuff like coding style, indention, heading, and curly braces.
/**
* Test.java
* CS219. Exception Handling practice
*
* If input is of wrong format, or if 0 is provided for divisor (the 2nd input),
* keep asking until valid data is provided.
*/
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
double dividend;
int divisor;
// input
System.out.print("Enter dividend: ");
dividend = stdIn.nextDouble();
System.out.print("Enter divisor: ");
divisor = stdIn.nextInt();
stdIn.close();
// process and result
System.out.println(dividend + " / " + divisor + " = " + dividend / divisor);
} // end main
} // end Test class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started