Core Java (3) JAVA input and output

Source: Internet
Author: User

A simple JAVA input/output instance contains the most common input/output functions.

Package COM. xujin; import Java. util. date; import Java. util. failed; public class mytest {static partition CIN = new partition (system. in); public static void main (string [] ARGs) {system. out. print ("input your name:"); string name = cin. next (); // read the next word, using space as the standard system. out. print ("input your age:"); int age = cin. nextint (); // read the next int, using space as the standard system. out. print ("input your salary:"); double salary = cin. nextdouble (); syst Em. out. print ("input detail:"); string detail = cin. nextline (); // read the next line. Here is the last press ENTER detail = cin. nextline (); // read the next line // format the output system. out. printf ("Name: % s \ n", name); system. out. printf ("Age: % d \ toct: % <O \ thex: % 1 $ # x \ n", age); // output: Age: 20oct: 24hex: 0x14system. out. printf ("Salary: % #, 8.2f \ ndetail:", salary); system. out. println (detail); // Time Output System. out. printf ("now: % TC \ n", new date (); // output: Wednesday, January 1, January 16 21:02:10 CST 201 3system. out. printf ("% 1 $ S % 2 $ ty % 2 $ TB % 2 $ TD \ n", "now:", new date (); // output: 2013 January 16 system. out. printf ("% S % ty % <TB % <TD \ n", "now:", new date (); // output: 2013 may January 16 // use a static string. the format method is a common formatted string without printing the output system. out. println (detail); string message = string. format ("Hello, % s! You will be % d next year ", name, ++ age); system. Out. Print (Message );}}

Console result:

Input your name: Gin
Input your age: 20
Input your salary: 5000
Input detail: detail...
Name: Gin
Age: 20 Oct: 24
HEX: 0x14
Salary: 5,000.00.
Detail: detail...
Now: Wednesday January 16 21:12:38 CST 2013
Now: September 2013
Now: September 2013
Detail...

Hello, gin! You will be 21 next year

Explanation:

System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20Oct:24Hex:0x14

% <O indicates formatting the preceding value. Age is displayed in octal.
% 1 $ # X indicates that the index is formatted as a value of 1 (1 $, first parameter, starting from 1), and the value is prefixed with 0x or 0 (# For X or O)

System.out.printf("Salary:%#,8.2f\nDetail:",salary);

Contains the decimal point (# To F). 8.2 indicates that the width is 8 characters and the accuracy of the two characters after the decimal point is printed.

Java format output, used after %

Conversion character used for printf
Conversion character Type
D Decimal integer
X Hexadecimal integer
O Octal integer
F Fixed Point floating point number
E Exponential floating point number
G General floating point number
A Hexadecimal floating point number
S String
C Character
B Boolean
H Hash code
TX Date and Time
% Percent sign
N Platform-related line delimiters

The flag used for printf. It is used after %.

  

Flag

Purpose

Example

+

Print the symbol before the number

+ 3333.33

Space

Add a space before a positive number.

| 1, 3333.33 |

0

Add 0 before the number

003333.33

-

Left aligned

| 1, 3333.33 |

(

Negative values are enclosed in parentheses.

(3333.33)

,

Add group Separator

3,333.33

# (For F)

Contains decimal point

3,333.

# (For X or O)

Add prefix 0x or 0

0 xcafe

^

Convert to uppercase

0 xcafe

$

Specify the format parameter index. For example, % 1 $ D and % 1 $ D indicate that the first parameter is printed in decimal and hexadecimal notation.

159 9f

<

Format the preceding parameters. For example, % d % <X indicates that the same parameter is printed in decimal and hexadecimal notation.

159 9f

Time and date Conversion characters:

Conversion character type example

C complete Date and Time mon Feb 09 18:05:19 PST 2004

F ISO 8601 date

D U.S. time format (mm/DD/year) 02/09/2004

T 24-hour time 18:05:19

06:05:19 R 12-hour time

R 24 hours no second time

Y four-digit year 2004

The last two digits of Y 04

The first two 20 in C

Complete spelling of month B: February

The abbreviation of B or H month is Feb.

M two months (add 0 before) 02

D two days (add 0 before) 09

E Date (do not add 0 before) 9

A complete Monday of the week

Mon

J. The number of days of the year, and the three digits are 0 069.

H: 24-hour, with two digits 0 and 18

K In 24-hour format. Do not add 0 to 18

I 12-hour system, with two digits 0 06

L 12-hour system, two do not add 0 6

M minutes, supplemented by 0 05

S second, complement 0 to 19

L millisecond, three digits 0 047

N in milliseconds, 9-bit complement 0 047000000

P upper-afternoon capital pm

P upper/lower case pm

Z rfc 822 numeric offset from GMT-0800

Z Time Zone PST

S 00:00:00 seconds from 1078884319

E 00:00:00 Ms count 1078884319047

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.