Java simple input / output

Keywords: Java

1, Scanner class

What is the Scanner class

The Scanner class is a basic class used in java for the interaction between users and computers;

How to use the Scanner class

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

import java.util.*;   // Guide bag

public class Test{

         public static void main(String[] args){
                     
                   String str;
                   int i;
                   double dou;

                   Scanner sc = new Scanner(System.in/*Represents standard input stream*/);
                             str = sc.next();    // Represents data of type String read in
                             i = sc.nexIn();  // Represents data of type Int read in
                             dou = sc.nexDouble(); // Represents data of type double read in

                             str  = sc.nexLine(); // Reads in all input from the current line up to the end of the line and returns it as a string  
}  
}

So the next() method and nextLine() in the Scanner class are not both input characters. Do they return string types? What's the difference between them?

First of all, they all have something in common: each point will input an object and return a string, but remember that the next() method will only return the input string, how to understand? See the following code: import java.util. *;

public class Test11 {

	public static void main(String[] args) {
		
		String message;
Scanner sca = new Scanner(System.in); System.out.println("Enter a line of text:"); message = sca.next(); System.out.println("You entered: \' " + message +" \' ");
	}
}

/* output result:
      Enter a line of text:
      sdsddsfs sdas
      You entered:'sdsddsfs'
*/

Do you see the result? If you input a string, if you add a space in the string, the system will return the string before the space to you, and the nexLine() method returns the whole line of the input string. According to the previous program, if you use nexLine() to output the result: sdsddsfs sdas is the whole output

 

1, NumberFormat class and DecimalFormat class

1. Basic functions of NumberFormat class:

In the output of floating-point numbers, it may happen that the decimal number is too long. In fact, it cannot use so many decimal numbers. All the NumberFormat basic classes provide the formatting output function, which is defined in the java.text package

1.1. Common methods in NumberFormat class:

getInstance(): returns the default value format of the current default locale;
getCurrencyInstance(): returns the general format of the current default locale;
getNumberInstance(): returns the general 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 for the decimal part of the value;
setMinimumIntegerDigists(int): sets the minimum number of digits allowed for the integer part of the value;
import java.text.*;

public class Test12 {

	public static void main(String[] args) {
		
		double dou = 2511.51634564;
		double mydouble = 1.2345;
		double doub = 0.25;
		
		String mystring = NumberFormat.getInstance().format(dou);  
		// The call of this method is of String type, so it should be assigned to a variable of String type 
		
		String mys = NumberFormat.getCurrencyInstance().format(dou); 
		
		String myst = NumberFormat.getNumberInstance().format(dou);
		
		String mystr = NumberFormat.getPercentInstance().format(doub);
		
		System.out.println(mystring); // Returns the default value format of the current default locale: 2511.516
		System.out.println(mys);      // Returns the common format of the current default locale: £ 2511.52
		System.out.println(myst);     // Returns the common numeric format of the current default locale: 2511.516
		System.out.println(mystr);    // Returns the percentage format of the current default locale: 25%
	
		System.out.println("---------------------------------------");
		
		NumberFormat format = NumberFormat.getInstance();
		// Redefining the default value format of the current default locale
		
		format.setMaximumFractionDigits(3);
		format.setMaximumIntegerDigits(4);
		format.setMinimumFractionDigits(1);
		format.setMinimumIntegerDigits(1);
		
		System.out.println(format.format(1234564.546484)); // 4,564.546
		
	} 
}

It is important to note that for the NumberFormat class, the object of the class is not created before calling the method in the class;

The important reason is that the NumberFormat class itself cannot use the new operator to create an instantiated object (NumberFormat). 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 parameter into a string return

But it's a bit troublesome to understand the program, because if you want to get several decimal places of a floating-point number, you have to rewrite the format of the default value, and then you can output several decimal places of the floating-point number you want, Then is there a simple way to output the decimal number without rewriting (the following program contains the method of rewriting the default numerical format):

import java.text.*;
import java.math.*;


public class Test13 {

	double dou = 1.2345;
	
	// The first method, however, is out of date
	public void m1() {
		
		BigDecimal bg = new BigDecimal(dou);
		double f1 = bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(f1);
	}
	
	// The second method
	public static void m2() {
		// Non static member variables cannot be referenced if a static method is defined
	    // So a static local variable is redefined here
		double dou = 1.12354;  
	
		String st = String.format("%.2f",dou);
		System.out.println(st);
	}
	
	// The third method
	public void m3() {
		
		DecimalFormat df = new DecimalFormat("#.00");
		System.out.println(df.format(dou));
	}
	
	// The fourth method
	public void m4() {
	
		NumberFormat nf = NumberFormat.getInstance();  // First, call up the default value format of the default locale
		nf.setMaximumFractionDigits(2); // The default value format is then rewritten to retain two decimal places
		
		System.out.println(nf.format(dou)); 
	}
	
	public static void main(String[] args) {
		
		// create object
		Test13 t = new Test13(); 
		
		// Calling method
		t.m1();
		
		// If there is a static method in the same class, the method can be called directly by using the method name
		m2();
		t.m3();
		t.m4();
	}
}

 

2, DecimalFormat class

DecimalFormat class is also a basic class for changing floating-point number. The biggest difference between DecimalFormat class and NumberFormat class is that DecimalFormat class needs new operator to create an instantiation object

 1  1 import java.text.*;
 2  2 
 3  3 public class Test14 {
 4  4 
 5  5     public static void main(String[] args) {
 6  6         
 7  7         double dou = 3.1415926;
 8  8         
 9  9         DecimalFormat df = new DecimalFormat("0.##");  // Remember that the way to define the number of digits to keep is to add them during object creation "#.00"the latter"0.##", never forget the double quotes
10 10 
11 11         System.out.println(df.format(dou));
12 12     }
13 13 }

Posted by Fakcon on Wed, 01 Apr 2020 07:07:18 -0700