import java.util.Scanner; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class ScannerDemo1 { public static void main(String[] args) { System.out.println("Type your age, temperature, first name."); System.out.println("Separate them with white space "+ "and follow the last one with Enter."); /* The Scanner object holds a referecne to System.in. The nextInt method of this scanner object will call the read method of System.in to get the digits of the number. */ Scanner scanner = new Scanner(System.in); try { final int age = scanner.nextInt(); final double temperature = scanner.nextDouble(); final String name = scanner.next(); //any string System.out.println("age == " + age + ", temperature == " + temperature + ", name == " + name); scanner.close(); } catch (InputMismatchException e) { System.err.println("wrong data type, " + "or value out of range"); System.err.println(e.getMessage()); System.exit(1); } catch (NoSuchElementException e) { System.err.println("input is exhausted"); System.err.println(e.getMessage()); System.exit(1); } catch (IllegalStateException e) { System.err.println("scanner already closed"); System.err.println(e.getMessage()); System.exit(1); } } }