Get the input value from the keyboard in Java

Source: Internet
Author: User

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, system. Out. Read () can only be obtained for one character. At the same time, the type of the obtained variables can only be

CHAR: when we enter a number and want to get an integer variable, We have to modify the variable type, which is troublesome.

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.

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 sectionCodeIt has been indicated that the category class can implement functions whether it is a string, an integer, or a float type variable, you only need to make a small change! No doubt he is the most powerful!

Related Article

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.