modify the java program below to use try-catch logic to detect input errors and display an error message instead of aborting the program.
modify the java
instead of aborting the program.
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String len, wid;
Double dblLen, dblWid, area;
len = JOptionPane.showInputDialog("Enter a value for the length:");
wid = JOptionPane.showInputDialog("Enter a value for the width:");
dblLen=Double.parseDouble(len);
dblWid=Double.parseDouble(wid);
area=dblLen*dblWid;
JOptionPane.showMessageDialog(null,"The area is "+area);
}
}
Given code:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String len, wid;
Double dblLen, dblWid, area;
len = JOptionPane.showInputDialog("Enter a value for the length:");
wid = JOptionPane.showInputDialog("Enter a value for the width:");
dblLen=Double.parseDouble(len);
dblWid=Double.parseDouble(wid);
area=dblLen*dblWid;
JOptionPane.showMessageDialog(null,"The area is "+area);
}
}
Step by step
Solved in 4 steps with 7 images