import java.util.Scanner; public class ScannerDemo2 { 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."); Scanner scanner = new Scanner(System.in); int age = 0; //make sure initialized double temperature = 0.0; String name = ""; if (scanner.hasNextInt()) { age = scanner.nextInt(); } else { System.err.print("Expected age, not "); if (scanner.hasNext()) { System.err.println(scanner.next() + "."); } else { System.err.println("end of input."); } System.exit(1); } if (scanner.hasNextDouble()) { temperature = scanner.nextDouble(); } else { System.err.print("Expected temperature, not "); if (scanner.hasNext()) { System.err.println(scanner.next() + "."); } else { System.err.println("end of input."); } System.exit(1); } if (scanner.hasNext()) { //any value at all name = scanner.next(); } else { System.err.println("Expected name, not end of input."); System.exit(1); } System.out.println("age == " + age + ", temperature == " + temperature + ", name == " + name); } }