When the program needs to obtain the commands or data entered by the user from the keyboard, for example: obtain the calculation expression entered by the user. User input can be easily obtained through the Scanner class.
When obtaining user input through the Scanner class, the console will wait for the user's input until the user hits the Enter key to complete the input. The content is passed to the Scanner. If the program wants to obtain the input content from the Scanner, it only needs to call the Scanner's nextLine() method. (Recommended learning: java course)
Initialization of the Scanner class
Declare a scanner variable and instantiate the Scanner using the new operator , when instantiating Scanner, you need to pass in the System.in object. Scanner obtains user input through the passed in System.in and processes the characters entered by the user, shielding the complex operation of obtaining user input.
Scanner scanner = new Scanner(System.in);
Next we demonstrate the simplest data input and obtain the input string through the next() and nextLine() methods of the Scanner class. Before reading, we generally need to use hasNext and hasNextLine Determine whether there is still input data:
Use next method:
ScannerDemo.java file code:
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // next方式接收字符串 System.out.println("next方式接收:"); // 判断是否还有输入 if (scan.hasNext()) { String str1 = scan.next(); System.out.println("输入的数据为:" + str1); } scan.close(); } }
The output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo next方式接收: runoob com 输入的数据为:runoob
The above is the detailed content of How to use keyboard input in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.