Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to receive characters from Keyboard in java

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to receive the characters in the keyboard in java". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to receive characters in the keyboard in java" can help you solve the problem.

Method 1:

Next () method

First, we create a new .java file in eclipse in java's development tool, declare a Scanner object in the code, and then use the if statement to determine whether there is any input. The code and screenshot are as follows:

Import java.util.*;public class Inputer {public static void main (String [] args) {Scanner scan = new Scanner (System.in); / / receive data System.out.println from the keyboard ("receive input using next method:"); / / receive string in next mode / / determine whether there is an input if (scan.hasNext ()) {String str1 = scan.next () System.out.println ("output data that receives input using the next method is:" + str1);}

In the screenshot, the green font is a character entered by our keyboard, and we can see the characters entered in our keyboard in the output, indicating the success of receiving the input from the keyboard.

Method 2:

NextLine () method

When we use this method the same as the method above, but we don't use if statements, let's look at our code and screenshots between them!

Import java.util.*;public class Inputer {public static void main (String [] args) {Scanner recive_inputer=new Scanner (System.in); String In_p = null; System.out.println ("receive keyboard input string:"); In_p = recive_inputer.nextLine (); System.out.println ("output keyboard input string:" + In_p);}}

From the screenshot, we can see that the method of using nextLine () is the same as that of next (). Can receive keyboard input.

Difference:

In next (), we must enter valid characters when typing, because the input will not end if no valid characters are read during the reading. And it can be removed automatically if a blank character is encountered when reading valid characters.

So the Terminator in nextLine () is Enter, so if we use this method, the return value is all the characters before entering the carriage return. And you can leave a blank character.

This is the end of the introduction on "how to receive characters in the keyboard in java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report