Home > Java > javaTutorial > body text

How to use java scanner

(*-*)浩
Release: 2022-01-12 15:26:55
Original
59230 people have browsed it

java.util.Scanner is a new feature of Java5. We can obtain user input through the Scanner class.

How to use java scanner

The following is the basic syntax for creating a Scanner object:

Scanner s = new Scanner(System.in);
Copy after login

Next we demonstrate the simplest data input and pass next() of the Scanner class Use the nextLine() method to obtain the input string. Before reading, we generally need to use hasNext and hasNextLine to determine whether there is still input data:

Use the next method:

import java.util.Scanner; 
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);        // 从键盘接收数据
 
        // next方式接收字符串
        System.out.println("next方式接收:");        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();            
            System.out.println("输入的数据为:" + str1);        
         }
        scan.close();    
      }
}
Copy after login

The output result of executing the above program is:

next方式接收:
runoob com
输入的数据为:runoob
Copy after login

You can see that the com string is not output, let’s look at nextLine next.

Use nextLine method:

import java.util.Scanner;
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if (scan.hasNextLine()) {
            String str2 = scan.nextLine();
            System.out.println("输入的数据为:" + str2);
        }
        scan.close();
    }
}
Copy after login

The output result of executing the above program is:

nextLine方式接收:
runoob com
输入的数据为:runoob com
Copy after login

You can see the com string output .

The difference between next() and nextLine()

next():

1. Be sure to read it to be valid characters before the input can be completed.

2. The next() method will automatically remove the blanks encountered before entering valid characters.

3. Only after entering valid characters, use the blank space entered after them as the separator or terminator.

next() cannot get a string with spaces.

nextLine():

1. Enter is the end character, which means that the nextLine() method returns all characters before entering.

2. Can get blank space.

The above is the detailed content of How to use java scanner. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!