Obtain the keyboard input value in Java [reprint]

Source: Internet
Author: User
In the process of program development, it is common to obtain the input value from the keyboard, but Java does not provide scanf () as the C language provides us (), c ++ provides us with a ready-made function to obtain the input value of the keyboard! If Java does not provide such a function, it does not mean we are helpless in this case. Please refer to the following three solutions:
Several methods are listed below:
Method 1: receive a character from the console and print it out.
Public static void main (string [] ARGs) throws ioexception {
System. Out. Print ("enter a char :");
Char I = (char) system. In. Read ();
System. Out. println ("your char is:" + I );
}
}
Although this method obtains the input characters from the keyboard. out. read () can only be obtained for one character. At the same time, the retrieved variable type can only be char. When we enter a number and want to obtain an integer variable, we have to modify the variable type, which makes it quite troublesome.
Method 2: receive a string from the console and print it out.In this question, we need to use the bufferedreader class and inputstreamreader class.
Public static void main (string [] ARGs) throws ioexception {
Bufferedreader BR = new bufferedreader (New inputstreamreader (system. In ));
String STR = NULL;
System. Out. println ("Enter your value :");
STR = Br. Readline ();
System. Out. println ("your value is:" + Str );
}
In this way, we can obtain the input string.
Method 3: I think this method is the simplest and most powerful, that is, the use of the struct class.
Public static void main (string [] ARGs ){
Pipeline SC = new pipeline (system. In );
System. Out. println ("enter your name :");
String name = SC. nextline ();
System. Out. println ("Enter your age :");
Int age = SC. nextint ();
System. Out. println ("Enter your salary :");
Float salary = SC. nextfloat ();
System. Out. println ("your information is as follows :");
System. Out. println ("name:" + name + "\ n" + "Age:" + age + "\ n" + "Salary:" + salary );
}
This Code already shows that the functions can be implemented by making a slight change to variables of the string, integer, or float type! No doubt he is the most powerful!

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.