Untitled
unknown
plain_text
2 years ago
1.7 kB
8
Indexable
import java.util.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.Scanner; public class ExceptionHandling { public static long handled(long x) { final Scanner sc = new Scanner(System.in); long id = 0; while (true) { try { id = sc.nextLong(); sc.nextLine(); break; } catch (InputMismatchException e) { sc.nextLine(); System.out.println("Enter the value in correct data type"); } } return id; } public static String handled(String x) { final Scanner sc = new Scanner(System.in); String name = null; while (true) { try { name = sc.nextLine(); break; } catch (InputMismatchException e) { System.out.println("Enter the value in correct data type"); } } return name; } public static LocalDate handled(LocalDate x) { final Scanner sc = new Scanner(System.in); boolean flag = false; LocalDate FromDate = null; while (true) { try { System.out.print("Year: "); int dy = sc.nextInt(); System.out.print("Month: "); int dm = sc.nextInt(); System.out.print("Date: "); int dd = sc.nextInt(); FromDate = LocalDate.of(dy, dm, dd); sc.nextLine(); break; } catch (InputMismatchException e) { System.out.println("Enter the value in correct data type"); } } return FromDate; } public static int handled(int x) { final Scanner sc = new Scanner(System.in); int id = 0; while (true) { try { id = sc.nextInt(); return id; } catch (InputMismatchException e) { sc.nextLine(); System.out.println("Enter the value in correct data type"); } } } }
Editor is loading...
Leave a Comment