forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerBug.java
More file actions
29 lines (23 loc) · 730 Bytes
/
ScannerBug.java
File metadata and controls
29 lines (23 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
/**
* Demonstrates a common problem using Scanner.
*/
public class ScannerBug {
public static void main(String[] args) {
String name;
int age;
Scanner in = new Scanner(System.in);
System.out.print("What is your name? ");
name = in.nextLine();
System.out.print("What is your age? ");
age = in.nextInt();
System.out.printf("Hello %s, age %d\n", name, age);
in.reset();
System.out.println();
System.out.print("What is your age? ");
age = in.nextInt();
System.out.print("What is your name? ");
name = in.nextLine();
System.out.printf("Hello %s, age %d\n", name, age);
}
}