Summary of methods for getting keyboard input in Java

Source: Internet
Author: User

Java program development process, need to get input values from the keyboard is often the case, but Java it is not like the C language to provide us with scanf (), C + + gives us the CIN () to get the keyboard input value of the ready-made function! Here are three ways to solve the problem:

method One: receive a character from the console and print it out

import java.io.*;
 Public Static void throws
         System.out.print ("Enter a Char:
         char i = (char
         System.out.println ("your char is:

Although this method implements the input character from the keyboard, System.out.read () can only be obtained for one character, and the type of the variable entered is only char, and when we enter a number, we want to get an integer variable, We also have to modify the variable types, which makes it more cumbersome.

method Two: receive a string from the console and print it out. In this topic, we need to use the BufferedReader class and the InputStreamReader class

import java.io.*;
 Public Static void throws
           New BufferedReader (new
           Null
           System.out.println ("Enter your value:
           
           System.out.println ("Your value is:
}

This will get the string we have entered.

method Three: This method I think is the simplest, the most powerful, is to use the scanner class

import Java.util.Scanner;
 Public Static void
         New
         System.out.println (" Please enter your name:
         
         System.out.println (" Please enter your age:
         Int
         System.out.println (" Please enter your salary:
         Float
         System.out.println (" Your information is as follows:
         System.out.println (" name:" +name+ "\ n" + " Age:" +age+ "\ n" + " Salary:
}

This code has shown that thescanner class, whether it's a string or integer data, or a variable of type float, only needs a little bit of change to be able to implement the function ! No doubt he is the most powerful!

However, when using the third input method, there is a need to note that the nextline () function, in the IO package has a function of the same functions as I next () function , they function like, but in the implementation of what difference, Take a look at the following code:

 Public Static void
         New
         System.out.println (" Please enter your age:
         Int
         System.out.println (" Please enter your name:
         
         System.out.println (" Please enter your salary:
         Float
         System.out.println (" Your information is as follows:
         System.out.println (" name:" +name+ "\ n" + " Age:" +age+ "\ n" + " Salary:
}

This code differs from the example code given in the third implementation of the input method above, in which the code executes nextinit () and then executes Nextline (), while the third method is performed nextline () first, then Nextinit (), When you are running two pieces of code, you will find the third method of the example can achieve normal input, and this code is entered age, after hitting enter key, skip the input name, directly to the input salary here, (you can run the code to see) This is why? In fact, after executing the nextinit () function, hitting the Enter enter key, the carriage return will be absorbed by the nextline () function, which actually executes the nextline () function to absorb the input carriage return (not without executing the nextline function), mentioned earlier and Nextline () functions the same function next (), their difference is that: next () function will not receive carriage return and tab, or space bar, etc., so in the use of nextline () function, pay attention to the strike of the carriage return is absorbed by it, causing the program to appear bug!!!

Finally, a small summary of the difference between next () and nextline ():

In Java, the next () method is not to receive the space, before the receipt of valid data, all the space or TAB key input is ignored, if there is valid data, you encounter these keys exit. Nextline () can receive a space or TAB key, and its input should end with the ENTER key.

Summary of methods for getting keyboard input in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.