Java input and output

Source: Internet
Author: User

1. Enter:
Format 1:scanner sc = new Scanner (new Bufferedinputstream (system.in));
Format 2:scanner sc = new Scanner (system.in);
In the case of a large amount of read data, format 1 will be faster.
Read an integer: int n = sc.nextint (); Equivalent to scanf ("%d", &n); or CIN >> N;
Read a string: string s = Sc.next (); Equivalent to scanf ("%s", s); or CIN >> s;
Read a floating-point number: Double t = sc.nextdouble (); Equivalent to scanf ("%lf", &t); or cin >> T;
Read a whole line: String s = sc.nextline (); Equivalent to gets (s); or Cin.getline (...);
Determine if there is a next input that can be sc.hasnext () or sc.hasnextint () or sc.hasnextdouble () or Sc.hasnextline ()

 Public Static voidMain (string[] args) {NumberFormat formatter=NewDecimalFormat ("000000"); String s= Formatter.format (-1234.567);//-001235System.out.println (s); Formatter=NewDecimalFormat ("# #"); S= Formatter.format (-1234.567);//-1235System.out.println (s); S= Formatter.format (0);//0System.out.println (s); Formatter=NewDecimalFormat ("# #00"); S= Formatter.format (0);//xxSystem.out.println (s); Formatter=NewDecimalFormat (". 00"); S= Formatter.format (-.567);//-.57System.out.println (s); Formatter=NewDecimalFormat ("0.00"); S= Formatter.format (-.567);//-0.57System.out.println (s); Formatter=NewDecimalFormat ("#.#"); S= Formatter.format (-1234.567);//-1234.6System.out.println (s); Formatter=NewDecimalFormat ("#.######"); S= Formatter.format (-1234.567);//-1234.567System.out.println (s); Formatter=NewDecimalFormat (". ######"); S= Formatter.format (-1234.567);//-1234.567System.out.println (s); Formatter=NewDecimalFormat ("#.000000"); S= Formatter.format (-1234.567);//-1234.567000System.out.println (s); Formatter=NewDecimalFormat ("#,###,###"); S= Formatter.format (-1234.567);//-1,235System.out.println (s); S= Formatter.format (-1234567.890);//-1,234,568System.out.println (s); //The ; Symbol is used to specify a alternate pattern for negative valuesFormatter =NewDecimalFormat ("#;(#)"); S= Formatter.format (-1234.567);//(1235)System.out.println (s); //The ' symbol is used to quote literal symbolsFormatter =NewDecimalFormat ("' # ' #"); S= Formatter.format (-1234.567);//-#1235System.out.println (s); Formatter=NewDecimalFormat ("' ABC ' #"); S= Formatter.format (-1234.567);//-ABC 1235System.out.println (s); Formatter=NewDecimalFormat ("#.##%"); S= Formatter.format (-12.5678987);  System.out.println (s); }  

Java input and output

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.