Adding in CS-115 coursework
This commit is contained in:
28
cs-115-test/src/ExceptionsDemo.java
Normal file
28
cs-115-test/src/ExceptionsDemo.java
Normal file
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user