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

How to enter a string in java

(*-*)浩
Release: 2019-11-13 10:10:15
Original
43036 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:
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template