-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReglaConScanner.java
More file actions
32 lines (26 loc) · 913 Bytes
/
Copy pathReglaConScanner.java
File metadata and controls
32 lines (26 loc) · 913 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
30
31
32
/**
* Regla a tener en cuenta en la utilización de la Clase Scanner
*/
import java.util.Scanner;
public class ReglaConScanner {
public static void main(String[] args) {
int age;
String name;
Scanner in = new Scanner(System.in);
System.out.print("What is your age? ");
age = in.nextInt();
/*
* Lo siguiente es necesario si se lee un int o double antes de un
* String, porque Scanner, al usar el método nextLine() lo primero que
* va a leer es precisamente un \n, así que de no poner esto, retornaría
* un String vacío, pues nextLine() lee hasta el próximo \n.
*
* Si Scanner va a leer un String y luego un int o double, no hay
* necesidad de indicar in.nextLine() entre medias.
*/
in.nextLine();
System.out.print("What is your name? ");
name = in.nextLine();
System.out.printf("Hello %s, age %d.\n", name, age);
}
}