How to use Scanner(System.in)

Trạng thái
Chủ đề đã được đóng (không tiếp nhận thảo luận)

Joe

Thành viên VIP
21/1/13
2,963
1,307
113
Hi
Time for something very basic. The usage of Scanner(System.in). Most newbies or JAVA beginners usually start to write a little app using Scanner(System.in) and System.out.println() to do the IO chores. And I dare to say that all of them run straight into problem with mixed IO between values and String. Example
PHP:
import java.util.Scanner; // <<--include this class
1 public class Person_1 {
2    public static void main(String[] args) {
3        Scanner input = new Scanner(System.in);
4        System.out.print("Nhap ten: ");
5        String name = input.nextLine();                 // <---nextLine()
6        System.out.print("Nhap tuoi: ");
7        int age = input.nextInt();                         // <---may cause trouble
8        System.out.print("Nhap dia chi: ");
9        String address = input.nextLine();              // <---the trouble
10       System.out.println(name + " -- " + age + " -- " + address);
    }
}
The problem is that Scanner for values reads the values that are separated by
1) a blank (space), or
2) a NewLine (Windows: \r\n and Linux: \n)
if there are several values that are needed to be "scanned". If Line 7 is given wit ONE value which is usually the case then the System.in buffer will be as following
Code:
<value><NewLine>
On Windows, for example, 10 and \r\n. And
PHP:
int age input.nextInt(); // will be 10
the rest in System.in buffer is the NewLine (\r\n). The input after line 8 is, for example, "San Diego". The System.in buffer would be
Code:
\r\nSan Diego\r\n
and the next "scan" in line 9
PHP:
String address = input.nextLine(); // address gets the NewLine or \r\n
takes \r\n and NOT "San Diego".
The following picture shows you how the Scanner works:

scanner.png

The given codes can be amended as following
PHP:
import java.util.Scanner; // <<--include this class
public class Person_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Nhap ten: ");
        String name = input.nextLine();                 // <---nextLine()
        System.out.print("Nhap tuoi: ");
        int age = Integer.parseInt(input.nextLine());   // <---safe way read the whole line
        System.out.print("Nhap dia chi: ");
        String address = input.nextLine();
        System.out.println(name + " -- " + age + " -- " + address);
    }
}
or more elegant:
PHP:
import java.util.Scanner; // <<--include this class
public class Person_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Nhap ten: ");
        String name = input.nextLine();                 // <---nextLine()
        System.out.print("Nhap tuoi: ");
        int age = input.nextInt();                         // <---as usual nextInt()
        input.nextLine();                                     // <<<<< jump over NewLine
        System.out.print("Nhap dia chi: ");
        String address = input.nextLine();              // <---OK now
        System.out.println(name + " -- " + age + " -- " + address);
    }
}
As you see, if you understand the structure and how Computer Science works you would see that everything was very simple and clear. The mentioned example shows you in general how a computer buffer works. Nothing gets lost. It depends only on how the implementation is made. Scanner reads a buffer (regardless of wherever) and is implemented precisely as the names of its methods:
- nextInt(), nextDouble(), etc. read a value that is ended by a blank or a NewLine
- nextLine() reads the whole line, incl. NewLine (which is then removed from the string)
 
Sửa lần cuối:
  • Like
Reactions: devtttq and JackV
Trạng thái
Chủ đề đã được đóng (không tiếp nhận thảo luận)