Home  >  Article  >  Java  >  How to enter a string in java

How to enter a string in java

(*-*)浩
(*-*)浩Original
2019-11-13 10:10:1542896browse

How to enter a string in java

First, import the java.util.* package. ​ ​ (Recommended learning: java course)

import java.util.*;

Then, you need to create a new scanner object that reads standard input (keyboard).

Scanner in = new Scanner(System.in);

Now, you can enter strings from the keyboard.

String s = in.nextLine();

The above line reads a line of string input from the keyboard into the variable s.

Please see a complete simple example:

import java.util.*;
public class Main
    {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        System.out.println(s);
    }
}

The above is the detailed content of How to enter a string in java. 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