Create a DataEntryException class whose getMessage() method returns information
ID: 3691619 • Letter: C
Question
Create a DataEntryException class whose getMessage() method returns information about invalid integer data.
Write a program named GetIdAndAge that continually prompts the user for an ID number and an AGE until a terminal 0 is entered for both.
Throw DataEntryException if the ID is not in the range of valid ID numbers(0 through 999), or if the age is not in the range of valid ages(0 through 119).
Catch any DataEntryException or InputMismatchException that is thrown, and display an appropriate message.
Save the files as DataEntryException.java and GetIDAndAge.java.
Explanation / Answer
ANS;
package MyPrograms.Finals;
public class DataEntryException extends Exception
{
public DataEntryException(String alertID)
{
super(alertID);
}
}
package MyPrograms.Finals;
import javax.swing.*;
public class GetIDAndAge
{
public static void main(String[] args)
{
int idNum = 1;
int age = 1;
String alertID = "ID is not in the range of valid ID numbers. Valid ID number (0 through 999)";
String alertAge = "Age is not in the range of valid AGE. Valid AGE number (0 through 119)";
String userInput;
while (idNum != 0 && age != 0)
{
try
{
userInput = JOptionPane.showInputDialog(null, "Enter ID number (Enter 0 to Quit.)");
idNum = Integer.parseInt(userInput);
if (idNum > 999 || idNum < 0)
{
throw (new DataEntryException(alertID));
}
userInput = JOptionPane.showInputDialog(null, "Enter age (Enter 0 to Quit.)");
age = Integer.parseInt(userInput);
if (age > 119 || age < 0)
{
throw (new DataEntryException(alertAge));
}
}
catch (DataEntryException exception)
{
JOptionPane.showMessageDialog(null, exception.getMessage());
}
catch (NumberFormatException exception)
{
JOptionPane.showMessageDialog(null, "This application accepts digits only!");
idNum = 1;
age = 1;
}
}
}
}