Home > Java > JavaBase > How to let Java get the string entered by the user

How to let Java get the string entered by the user

王林
Release: 2019-12-06 14:54:33
Original
7205 people have browsed it

How to let Java get the string entered by the user

It is a very common operation to dynamically input data from the console, perform various processing on the data, and then output the data. Now we will give a systematic introduction to the data input method:

Call of Scanner class

Related methods:

hasNext() Determine whether there is the next paragraph after the current scanning position in the scanner.

hasNextLine()Returns true if another line exists in this scanner's input.

next()Find and return the next complete token from this scanner.

nextLine()This scanner executes the current line and returns skipped input information.

nextInt()Return the integer data scanned by the console.

Related video tutorial sharing: java learning video

Code example:

package FIRST_Chapter;
import java.util.Scanner;
public class TestScanner { 
        public static void main(String[] args) { 
                Scanner s = new Scanner(System.in); 
                System.out.println("请输入字符串:"); 
                while (true) { 
                        String line = s.nextLine(); 
                        if (line.equals("ok")) break; 
                        System.out.println(">>>" + line); 
                } 
        } 
}
Copy after login

Result demonstration:

How to let Java get the string entered by the user

The difference between next() and nextLine() methods

next() method must read valid characters before it can end the input. Valid characters must be entered The next() method will automatically remove the previously encountered terminators such as the space bar, Tab key or Enter key. Only after entering a valid character, the next() method will remove the space bar, Tab key or Enter key entered afterwards. Keys, etc. are treated as delimiters or terminators.

Simply put, next() finds and returns the next complete token from this scanner. The complete tag is preceded and followed by input information that matches the delimited pattern, so the next method cannot get a string with spaces.

The end character of the nextLine() method is only the Enter key, that is, the nextLine() method returns all the characters before the Enter key, and it can get a string with spaces.

For example, the following program:

package FIRST_Chapter;

import java.util.Scanner;

class test{
    public static void main(String[] Args){

        Scanner sc= new Scanner(System.in);
        System.out.println("请输入一段数据");
        String str =sc.next();
        System.out.println("用next输入的语句"+str);
        String str1 =sc.nextLine();
        System.out.println("用nextLine输入的语句"+str1);
        //sc.nextLine();//如果下面注释行想用nextLine的话,就要注意加上这句话

    }
}
Copy after login

Output result:

How to let Java get the string entered by the user

## Change the result output:

How to let Java get the string entered by the user

Summary: Next encounters a space or a newline to end the input. NextLine will only end the input with a line feed (carriage return).

It can be seen from the second result that when you enter the carriage return to indicate the end of the input, the next line of code nextLine also ends the input at this time. The input result is empty, just a carriage return.

Recommended related articles and tutorials:

java introductory tutorial

The above is the detailed content of How to let Java get the string entered by the user. 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