import java.util.*; // Class to demonstrate exceptions using the Scanner class. public class ScannerDemo { // Start of program. public static void main(String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println("Delimiter pattern = " + scan.delimiter() + "\n"); byte b; double d; float f; int i; long l; boolean t; while (true) { System.out.println("Enter the input type (b, d, f, i, l, t, or q):"); message = scan.nextLine(); System.out.println("You entered: \"" + message + "\""); if (message.toLowerCase().equals("q")) System.exit(1); System.out.println("Enter matching data"); try { if (message.length()==0) throw new NoSuchElementException(); switch (message.charAt(0)) { case 'b': b = scan.nextByte(); System.out.println("byte = " + b); break; case 'd': d = scan.nextDouble(); System.out.println("double = " + d); break; case 'f': f = scan.nextFloat(); System.out.println("float = " + f); break; case 'i': i = scan.nextInt(); System.out.println("int = " + i); break; case 'l': l = scan.nextLong(); System.out.println("long = " + l); break; case 't': t = scan.nextBoolean(); System.out.println("boolean = " + t); break; default: throw new InputMismatchException(); } scan.nextLine(); // Move past the newline character. } catch (InputMismatchException mme) { System.out.println("Wrong Type"); scan.nextLine(); // Move past the newline character. } catch (NoSuchElementException nse) { System.out.println("No Such Element"); } } // End while. } // End main. }