diff options
Diffstat (limited to 'java/code')
-rw-r--r-- | java/code/ShapeAreaCalculations.java | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/java/code/ShapeAreaCalculations.java b/java/code/ShapeAreaCalculations.java index 0d00bff..ca20d39 100644 --- a/java/code/ShapeAreaCalculations.java +++ b/java/code/ShapeAreaCalculations.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.InputMismatchException; class Shape { // Calculates the area of a triangle. @@ -20,11 +21,11 @@ class Shape { } } -class AreaCalc { +class ShapeAreaCalculations { public static void main(String args[]) { - var sc = new Scanner(System.in); + try (var sc = new Scanner(System.in)) { System.out.println("Choose the shape: "); - System.out.println("1. Triangle\n2. Rectangle\n3. Square\n4.Circle"); + System.out.println("1. Triangle\n2. Rectangle\n3. Square\n4. Circle"); System.out.print("Enter your choice: "); var choice = sc.nextInt(); switch (choice) { @@ -55,5 +56,8 @@ class AreaCalc { System.out.println("Invalid choice"); break; } + } catch (InputMismatchException e) { + System.err.println("Invalid number given as input"); + } } } |