Java keyboard input

Source: Internet
Author: User

When beginners learn Java, they sometimes need to receive input from the keyboard. It is easy to think of scanf () in C and CIN () in C ++. What can be used in Java? I checked many people on the internet and said that the use of system. In misled many people, including me, and someone said that the next plug-in was installed. In my opinion, the simplest way to implement it is actually a class, which Java itself provides. This class is actually a class. The following is an example:

Import java. util. collections;

/**

* Test the function of reading user input from the keyboard. The key class is "allow ".

* @ Author Administrator

*

*/

Public class testinput {

Public static void main (string [] ARGs ){

S = new partition (system. In );

System. Out. println ("enter your name :");

String name = S. nextline ();

System. Out. println ("Enter your age :");

Int age = S. nextint ();

System. Out. println ("Enter your salary :");

Float salary = S. nextfloat ();

System. Out. println ("your information is as follows :");

System. Out. println ("name:" + name + "\ n" + "Age:" + age + "\ n" + "Salary:" + salary );

}

}

Note: nextline () method of the objects to be merged receives character and string type input; nextint () receives int type; nextfloat () receives float type, I believe that you will not need to list them one by one.

Method 2:

Bufferedreader reader = new bufferedreader (New inputstreamreader (system. In ));

System. Out. Print ("Please input a number :");

String STR = reader. Readline (); // obtain the string

System. Out. println ("the number you input is:" + Str );

Or

Inputstreamreader reader = new inputstreamreader (system. In );

Bufferedreader input = new bufferedreader (Reader );

System. Out. Print ("Enter your age :");

String text = input. Readline ();

Int age = new INTEGER (text). intvalue ();

System. Out. println ("you are" + age + "years old, now ,");

Int year = 2003-age;

System. Out. println ("so you were probably born in" + year );

Io exception must be thrown.

Import java. Io .*;

-@ 5 N0 _ 4 h: C (Y: hclass javaio_02n {'u-\ 9 p1 S, C2 M-R3 P & T

Public static void main (string Param [])

# U p +] + A2 L6 o! V {: B7 A6 R3 ^ [4 ~

String nn1 = ""; // defines a string integer object nn1: B5 t) N6 E9 F4 @ 9 p

Bufferedreader distream = new bufferedreader (New inputstreamreader (system. In ));

, P1 j d/B, @ 1 L "Q6 p'a system. out. println ("type an integer"); R & M/H2 s 'q H "t (| + J

Nn1 = distream. Readline (); // input the number and store it in nn1/L, U4 Y3 E5 r \

) J + D7 N $ U; Z % V3 z7 G # S * W0 l int n1 = integer. parseint (nn1); (Z4 L # | (L/Q

If (N1% 2 = 0)

. C6 B9 U $ D * f (y! K system. Out. println (N1 + "is an even number ");

3 ?! ^ "C % C9], I-G9 A9}/B else (u-g, Z (z * {v # _; _ 0 o

System. Out. println (N1 + "is an odd number"); 2 _; B) n, Q2 \ 4 W9 D: Y $ t! E5 ~ # J

} % C! Z 'y (D6 D/V # {. _ 7 Q

}

1 F: w6 M1 h) | 8 J) O: E 'c4 E

& V "} 5 S9} & Y & l8 B

"F9 G8 K, H + A6 P % ajava cannot appear in any method, except the class

/** Created on 2005-4-5

** Todo to change the template for this generated file go

* Window-preferences-Java-code style-code templates

*/

Import java. Io. bufferedreader;

Import java. Io. ioexception;

Import java. Io. inputstreamreader;

/**

* @ Author zhangql

*

* Todo to change the template for this generated type comment go

* Window-preferences-Java-code style-code templates

*/

Public class enterconsole

{

Public static void main (string [] ARGs) throws ioexception

{

Enterconsole = new enterconsole ();

Enterconsole. printconsolechar ();

}

/**

* Receives a string from the control pair and then outputs it to the console.

* @ Throws ioexception

*/

Public void printconsoleline () 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 );

}

/**

* Receive a character from the console

* Print it to the console.

* @ Throws ioexception

*/

Public void printconsolechar () throws ioexception

{

System. Out. Print ("enter a char :");

Char I = (char) system. In. Read ();

System. Out. println ("your char is:" + I );

}

}

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.