Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the simple input / output methods of Java

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the simple input / output methods of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the simple input / output methods of Java.

1. Scanner class

What is the Scanner class?

The Scanner class is a basic class in java for users to interact with computers.

How to use the Scanner class

The Scanner class is stored in the java.util package and provides methods for users to use. The key point is that when you want to call a method in Scanner, you must first create an object (use the new operator to create the object), and the basic format of the call:

+ View Code

So aren't the next () method and nextLine () in the Scanner class both input characters that return a string type? Is there any difference between them?

First of all, they all have something in common: all points enter an object and the string returns, but remember that the next () method only returns the input string. Please look at the following code: import java.util.*

+ View Code

+ View Code

Did you see the results? If when you enter a string, if you add a space to the string, the system returns the string before the space, while the nexLine () method returns the entire line of the input string, according to the result output with nexLine () in the previous program: sdsddsfs sdas is the whole output.

2. NumberFormat class and DecimalFormat class

1. The basic functions of the NumberFormat class:

It is possible that the decimal is too long in the output of floating-point numbers, but in fact, you don't need so many decimals. All NumberFormat basic classes provide formatting output, which is defined in the java.text package.

1.1.Common methods in the NumberFormat class:

GetInstance (): returns the default format of the current default locale; getCurrencyInstance (): returns the common format of the current default locale; getNumberInstance (): returns the common numeric format of the current default locale; getPercentInstance (): returns the percentage format of the current default locale; setMaximumFractionDigits (int): sets the maximum number of digits allowed for the decimal part of the value SetMaximumIntegerDigits (int): sets the maximum number of digits allowed in the integer part of the value; setMinimumFractionDigits (int): sets the minimum number of digits allowed in the decimal part of the value; setMinimumIntegerDigists (int): sets the minimum number of digits allowed in the integer part of the value

+ View Code

It is worth noting, but also very important: for the NumberFormat class, the object of the class is not created before calling the method in its class

The very important reason is that the NumberFormat class itself cannot use the new operator to create an instantiated object (NumberFormat). Instead, it can only directly use the class name to call a special static method to get an object, and then use the object to call the format () method to convert the parameters to a string and return.

However, through the understanding of the program, it is always a bit troublesome, because if you want to get a few decimal places of a floating point number, you must rewrite the format of the default value before you can output the number of decimal places you want to float. Is there an easy way to output the number of decimal places you want without rewriting (the following program includes the method of rewriting the default numerical format):

+ View Code

Third, DecimalFormat class

The DecimalFormat class is also a basic class for changing floating-point digital amounts, and the biggest difference between the DecimalFormat class and the NumberFormat class is that the DecimalFormat class requires the new operator to create an instantiated object.

Thank you for your reading, these are the contents of "what are the simple input / output methods of Java". After the study of this article, I believe you have a deeper understanding of what the simple input / output methods of Java have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report