This must be completed without JOPtionPane or GUI. Create a DataEntryException c
ID: 673260 • Letter: T
Question
This must be completed without JOPtionPane or GUI.
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 a DataEntryException if the ID is not in the range of valid 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 File as DataEntryException.java and GetIDAndAge.java.
Explanation / Answer
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;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.