sokolov_it

Exception handling -> Converting and multiplying

Nov 20th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. class Main {
  6.     static ArrayList<Object> list = new ArrayList<>();
  7.     static Scanner scanner = new Scanner(System.in);
  8.     static int x;
  9.  
  10.     public static void main(String[] args) {
  11.         try {
  12.             while (scanner.hasNext()) {
  13.                 input();
  14.                 if (x == 0) {
  15.                     break;
  16.                 }
  17.             }
  18.         } catch (InputMismatchException e) {
  19.             input();
  20.         }
  21.         list.forEach(System.out::println);
  22.     }
  23.  
  24.     public static void input() {
  25.         try {
  26.             while ((x = scanner.nextInt()) != 0) {
  27.                 list.add(x * 10);
  28.             }
  29.         } catch (InputMismatchException e) {
  30.             list.add("Invalid user input: " + scanner.next());
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment