From cd6a9243c056710794549a1ab5d51fbdd082ce2e Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sat, 27 Jan 2024 11:33:43 +0000 Subject: Complete Java assignments #7-13 --- java/code/ShapeAreaCalculations.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'java/code') 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"); + } } } -- cgit 1.4.1-2-gfad0