forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerBug.java
More file actions
26 lines (21 loc) · 679 Bytes
/
ScannerBug.java
File metadata and controls
26 lines (21 loc) · 679 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
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);
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);
}
}