Home > Java > JavaBase > body text

How to enter a string in java

(*-*)浩
Release: 2019-11-13 10:10:15
Original
42899 people have browsed it

How to enter a string in java

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

import java.util.*;
Copy after login

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

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

Now, you can enter strings from the keyboard.

String s = in.nextLine();
Copy after login

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);
    }
}
Copy after login

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!

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!