IO-04. Mixed Type data formatting Input

Source: Internet
Author: User

Write a program to read floating point 1, integer, character, and floating point 2 in sequence, and then output the data in the order of character, integer, floating point 1, and floating point 2.

Input Format:

InputIn a rowA floating point number 1, an integer, a character, and a floating point number 2 are given in sequence, which are separated by one space.

Output Format:

Output in the order of characters, integers, floating point numbers 1, and floating point number 2 in a row. The floating point number retains the last two digits of the decimal point.

Input example:
2.12 88 c 4.7
Output example:
c 88 2.12 4.70
/*
I did not write it myself, but later I found that I did not understand the meaning of the question! It must be "input a line" with a space added between them. I enter one line at a time!
Import Java. util. extends; public class main {public static void main (string [] ARGs) {extends input = new loads (system. in); float a = input. nextfloat (); int B = input. nextint (); string c = input. next (); float d = input. nextfloat (); system. out. printf ("% S % d %. 2f %. 2f ", C, B, A, D) ;}} */import Java. text. decimalformat; import Java. util. extends; public class main {public static void main (string [] ARGs) {S = new evaluate (system. in); string input = S. nextline (); If (input. split (""). length = 4) {// enter the floating point number 1, integer, character, floating point number 2 decimalformat fnum = new decimalformat ("#0.00") in sequence in a row "); float AA = float. parsefloat (input. split ("") [0]); string a = fnum. format (AA); int B = integer. parseint (input. split ("") [1]); string c = input. split ("") [2]; float dd = float. parsefloat (input. split ("") [3]); string d = fnum. format (dd); // output data in the order of characters, integers, floating point 1, and floating point 2 in a row. out. print (C + "" + B + "" + A + "" + d );}}}
Knowledge extension:
1. split method: Splits a string into substrings and returns the result as a string array.
Stringobj. Split ([separator, [limit])
Parameters
Stringobj
Required. String object or text to be decomposed. This object will not be modified by the split method.
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.
Limit
Optional. This value is used to limit the number of elements in the returned array.

Note:
The result of the split method is a string array. In stingobj, the position where separator appears must be decomposed.
Therefore, the normal syntax is as follows:
1) if ". ", must be written as follows: string. split ("\\. "), in this way, the correct separation is not allowed using string. split (". ");
2) if "|" is used as the separator, it must be written as follows: string. split ("\ |"), in order to correctly separate, cannot use string. split ("| ");
"." And "|" are escape characters and must be added "\\";
3) if a string contains multiple delimiters, you can use "|" as a hyphen, for example, "A = 1 and B = 2 or C = 3 ", you can use string to separate all three. split ("and | or ");
2. The decimalformat class is also a subclass of format. It mainly serves to format numbers.
Of course, it is more convenient to format a number than to directly use numberformat, because you can directly specify a custom format, similar to simpledateformat.
To customize formatting, you must specify a template for formatting, as shown in the following table:

Serial number

Mark

Bit

Description

1

0

Number

Each 0 represents an Arabic number,

If this bit does not exist, 0 is displayed.

2

#

Number

Represents an Arabic number. Each # represents an Arabic number,

If this bit does not exist, it is not displayed.

3

.

Number

Decimal separator or decimal separator of currency

4

-

Number

Negative number

5

,

Number

Group Separator

6

E

Number

Separate the ending number and index in scientific notation

7

;

Submode Boundary

Separate Positive and Negative submodes

8

%

Prefix or suffix

The number is multiplied by 100 and displayed as a percentage.

9

\ U2030

Prefix or suffix

Multiply by 1000 and display as a score

10

Bytes

\ U00a4

Prefix or suffix

Currency mark, which is replaced by the currency number. If both of them appear at the same time,

It is replaced by the international currency symbol. If it appears in a certain mode

, The decimal separator is used instead of the decimal separator.

11

,

Prefix or suffix

It is used to enclose special characters with quotation marks in the prefix or suffix, for example

& Quot; '# & quot; format 123 as & quot; #123 & quot ". Create a ticket

Number itself, two single quotes are used consecutively, for example, "# O '''clock"

The following example shows how to use the preceding formatting template.

Example: format a number

Package Org. lxh. demo11.numberdemo; import Java. text. decimalformat; Class formatdemo {public void format1 (string pattern, double value) {decimalformat df = NULL; // declare a decimalformat object df = new decimalformat (pattern ); // instantiate the object string STR = DF. format (value); // format the number system. out. println ("use" + pattern + "format number" + value + ":" + Str) ;}} public class numberformatdemo02 {public static void main (string [] ARGs) {formatdemo demo = new formatdemo (); demo. format1 ("###,###. ### ", 111222.34567); demo. format1 ("000,000.000", 11222.34567); demo. format1 ("###,###. ### ¥ ", 111222.34567); demo. format1 ("000,000.000 $", 11222.34567); demo. format1 ("##. ### % ", 0.345678); // demo in percent format. format1 ("00. ### % ", 0.0345678); // demo in percent format. format1 ("###. ### \ u2030 ", 0.345678); // use the thousands of scores }}
View code
// Program running result: Use ###,###. ### format a number 111222.34567: 111,222.346 use 000,000.000 format a number 11222.34567: 011,222.346 use ###,###. ### $ format the number 111222.34567: 111,222.346 $ format the number 000,000.000: 11222.34567 $ format the number 011,222.346: $ use ##. ### % format the number 0.345678: 34.568% 00. ###% format the number 0.0345678: 03.457% ###. ### format the number 0.345678: 345.678 ‰

 

IO-04. Mixed Type data formatting Input

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.