Files
cs-115-programming-in-java/cs-115-test/src/ExceptionsDemo.java
2025-09-27 14:53:33 -06:00

29 lines
1.0 KiB
Java

import javax.swing.*;
import java.util.InputMismatchException;
public class ExceptionsDemo {
public static void main(String[] args) {
int numerator = 0;
int denominator = 0;
int result;
String inputString;
try {
inputString = JOptionPane.showInputDialog(null, "Enter a number to be divided");
numerator = Integer.parseInt(inputString);
inputString = JOptionPane.showInputDialog(null, "Enter a number to divide into the first number");
denominator = Integer.parseInt(inputString);
result = numerator / denominator;
JOptionPane.showMessageDialog(null, numerator + " / " + denominator + "\nResult is " + result);
} catch (ArithmeticException exception) {
JOptionPane.showMessageDialog(null, exception.getMessage());
result = 0;
} catch (NumberFormatException exception) {
JOptionPane.showMessageDialog(null, "Please enter only Integers!");
result = 0;
}
}
}