Hey, I am very unfamiliar with exception handling and I need some help with this
ID: 3625065 • Letter: H
Question
Hey, I am very unfamiliar with exception handling and I need some help with this. Pls find my attempts below and correct them accordingly.The question prompt reads
Update the ZoningApp code below and add exception handling so the user cannot "break" your program with incorrect input format or values.
IT COMPILES BUT NOTHING HAPPENS
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.Math;
public class ZoningApp
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
//initialize variables
double length= 0.0;
double width = 0.0;
boolean invalid = false;
while (invalid == false)
{
try
{
length = in.nextDouble();
System.out.println("The length of the lot entered is: " + length);
width = in.nextDouble();
System.out.println("The width of the lot entered is: " + width);
}
catch ( InputMismatchException ex )
{
System.out.println("Error message: You entered invalid data");
invalid = true;
System.out.println("The program will re-run now") ;
}
}
System.out.println("Thank you, Goodbye for now");
//changing object and output
//instantiate an object of the zoning class
Zoning lot2 = new Zoning(length, width);
System.out.println("Non-Default Zoning Object");
System.out.println(lot2.toString()+ " ");
lot2.setLength(-1);
System.out.println("Test a negative length");
System.out.println(lot2.toString()+ " ");
}
}
IF the Zoning class itself is needed pls comment saying that. Thanks a lot in advance
Explanation / Answer
import java.io.*; import java.util.*; public class ZoningApp { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); //initialize variables double length = 0.0; double width = 0.0; boolean invalid = false; Zoning lot1 = new Zoning(); System.out.println("Default Zoning Object"); System.out.println(lot1.toString()+ " "); while (invalid == false) { try { System.out.println("Enter your lot length: "); length = in.nextDouble(); } catch ( InputMismatchException ex ) { System.out.println("Error message: You entered invalid data"); invalid = true; System.out.println("Please try again.") ; } } invalid=false; while (invalid == false) { try { System.out.println("Enter your lot width: "); width = in.nextDouble(); } catch ( InputMismatchException ex ) { System.out.println("Error message: You entered invalid data"); invalid = true; System.out.println("Please try again.") ; } } lot1.setLength(length); lot1.setWidth(width); System.out.println("Updated Zoning Object "); System.out.println(lot1.toString()+ " "); while (invalid == false) { try { System.out.println("Enter your lot length: "); length = in.nextDouble(); } catch ( InputMismatchException ex ) { System.out.println("Error message: You entered invalid data"); invalid = true; System.out.println("Please try again.") ; } } invalid=false; while (invalid == false) { try { System.out.println("Enter your lot width: "); width = in.nextDouble(); } catch ( InputMismatchException ex ) { System.out.println("Error message: You entered invalid data"); invalid = true; System.out.println("Please try again.") ; } } System.out.println("The length of the lot entered is: " + length); System.out.println("The width of the lot entered is: " + width); //instantiate an object of the zoning class Zoning lot2 = new Zoning(length, width); System.out.println("Non-Default Zoning Object"); System.out.println(lot2.toString()+ " "); lot2.setLength(-1); System.out.println("Test a negative length"); System.out.println(lot2.toString()+ " "); } }