search
HomeJavaJavaBaseHow to let Java get the string entered by the user

How to let Java get the string entered by the user

Dec 06, 2019 pm 02:54 PM
javastringObtainenter

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); 
                } 
        } 
}

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的话,就要注意加上这句话

    }
}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment