20 API 1-2 regular expression packing class auto boxing / Auto unpacking BigDecimal

Posted by phence on Sat, 25 Sep 2021 11:53:16 +0200

1 regular expression Regex

1.1 general

Correct string format rules.
It is often used to judge whether the content entered by the user meets the format requirements. Note that it is strictly case sensitive;

1.2 common syntax

1.3 String provides methods to support regular expressions

Matches: can the current string match a regular expression
Replaceall (regular, substring): replaces a substring
Split (regular): split strings

1.4 exercise: test the ID number.

package cn.api;

import java.util.Scanner;

/*This class is used to test regular expressions*/
//Requirement: prompt and receive the ID number entered by the user, and output the result of the judgement.
public class TestRegex {
    public static void main(String[] args) {
        //1. prompt and receive the ID number entered by the user.
        System.out.println("Please input your ID number:");
        String input = new Scanner(System.in).nextLine();

        //2. Edit regular expressions
        //The rule of ID number is 18, the first 17 are numbers, and the last one may be numbers, or X.
        //String regex = "[0-9]{17}[0-9X]";
        /*Single \ has a special meaning in java. It is considered as an escape symbol
        * So if you want to simply express that this is a \, you need to precede it with an escape\
        * That is\*/
        String regex = "\\d{17}[0-9X]";

        //3. Judge whether the data entered by the user conforms to the regular expression
        /*input It's the data we received
        * regex Is the regular expression we define, that is, the rule to match
        * matches()It is used to judge whether the input data conforms to the rules defined by regex*/
        if(input.matches(regex)){
            System.out.println("Congratulations! Correct input!");
        }else {
            System.out.println("Incorrect input!");
        }

    }
}

==matches() is used to determine whether the input data conforms to the rules defined by regex==

2 packaging

Package the basic types to provide more perfect functions.
The basic type has no function, just a variable and record value, while the wrapper class can have more functions

2.1 correspondence with basic types

2.2 Number

The abstract parent of the digital wrapper class.
Provides various ways to obtain values.

2.3 Integer

create object

Method 1: new Integer(5);
Mode 2: Integer.valueOf(5);
The Integer class contains 256 Integer cache objects, ranging from - 128 to 127
When valueOf() is used, if the value is within the specified range, the cache object will be accessed directly without creating a new one; If you specify a value outside the range, create a new object directly.

Common methods

static int parseInt(String s) parses string parameters as signed decimal integers

2.4 exercise: Integer of Number

package api;

import java.util.Scanner;

/* This class is used as an introductory case for regular expressions
*  Requirement: receiving the ID number entered by the user and exporting the result of the judgement.*/
public class TestRegex {
    public static void main(String[] args) {
        //1. prompt and receive the ID number entered by the user.
        System.out.println("Please enter your ID number:");
        String input = new Scanner(System.in).nextLine();

        //2. Edit regular expressions
        //The rule of the ID card number: the total number is 18, the first 17 numbers are numbers, the last one may be the number, and it may be X.
        //String regex = "[0-9]{17}[0-9x]";

        /*A single \ has a special meaning in java. It represents an escape symbol and is not considered a slash
        * So if you want to express a slash, you need to add an escape in front of it\
        * That is, \ \ is expressed as a simple slash*/
        String regex = "\\d{17}[0-9x]";



        //3. Judge whether the data entered by the user conforms to the regular expression
        if (input.matches(regex)){//Whether the data entered by the user matches the regex regular expression
            System.out.println("Correct input");
        }else{
            System.out.println("Input error");
        }
    }
}
package api;

import java.util.Scanner;

/* This class is used as an introductory case for regular expressions
*  Requirement: receiving the ID number entered by the user and exporting the result of the judgement.*/
public class TestRegex {
    public static void main(String[] args) {
        //1. prompt and receive the ID number entered by the user.
        System.out.println("Please enter your ID number:");
        String input = new Scanner(System.in).nextLine();

        //2. Edit regular expressions
        //The rule of ID number is 18, the first 17 are numbers, and the last one may be numbers, or X.
        //String regex = "[0-9]{17}[0-9x]";

        /*A single \ has a special meaning in java. It represents an escape symbol and is not considered a slash
        * So if you want to express a slash, you need to add an escape in front of it\
        * That is, \ \ is expressed as a simple slash*/
        String regex = "\\d{17}[0-9x]";



        //3. Judge whether the data entered by the user conforms to the regular expression
        if (input.matches(regex)){//Whether the data entered by the user matches the regex regular expression
            System.out.println("Correct input");
        }else{
            System.out.println("Input error");
        }
    }
}

2.5 Double

create object

new Double(3.14)
Double.valueOf(3.14) / / no difference from new

common method

Double.parseDouble();

Exercise: Double of Number

package cn.api;
/*This class is used to test packaging*/
public class TestNumber {
    //1. When defining member variables, pay attention to defining them as static, because static can only call static variables
    static Integer i0;//Member variables may not be initialized
    public static void main(String[] args) {
        
        //5. Create an object of double type corresponding to the wrapper class double
        Double d1 = new Double(3.14);
        Double d2 = Double.valueOf(3.14);
        Double d3 = Double.valueOf(3.14);
        System.out.println(d1 == d2);
        System.out.println(d2 == d3);//false
        /*Only the valueOf creation method of Integer and the data in - 128 ~ 127 can have an efficient effect
        * Double There is no efficient effect*/

        //6. Common test methods
        /*Reason: parseInt() is used to convert String type data to int
        * So "800" is converted to data 800 of type int and can participate in the operation*/
        System.out.println(i1.parseInt("800")+8);//808
        /*Reason: parseDouble() is used to convert String type data to double
         * So "2.4" is converted to double data and can participate in the operation*/
        System.out.println(d1.parseDouble("2.4")+1);//3.4

3 automatic packing and unpacking

3.1 general

Automatic packing: the process of packing basic types into corresponding packing types
Integer a = 5;//a is the reference type and refers to the address of the wrapper object.
The compiler will complete the automatic boxing of objects: Integer a = Integer.valueOf(5);

Automatic unpacking: automatically changes from the value of package type to the value of basic type
int i = a;//a is the packing type now. It is impossible to assign a value to the variable. You need to take out 5.
The compiler will complete automatic unpacking: int i = a.intValue();

3.2 exercise: automatic packing and automatic unpacking test

package cn.tedu.api;
/*This class is used to test automatic packing and unpacking*/
public class TestBox {
    public static void main(String[] args) {
        //1. Data defining package type
        //Review: there are two ways to create packaging types in the past
        Integer i1 = new Integer(127);
        Integer i2 = Integer.valueOf(127);
        //2. Current approach:
        /*1.Auto boxing: the compiler will automatically wrap the basic type int 5 into the wrapper type Integer
        * Then give it to i3 to save, and automatically pack the code Integer.valueOf(5) at the bottom;
        * valueOf()Direction of: int -- > integer*/
        Integer i3 = 5;//No error will be reported. This phenomenon is automatic packing
        /*2.Automatic unpacking: the compiler will automatically remove the "box" of i1 of packaging type and change it back to basic type data 127
        * Then give it to i4 to save. The code at the bottom of automatic unpacking: i1.intValue();
        * intValue()Direction of: integer - > int
        * */
        int i4 = i1;//No error will be reported. This phenomenon is automatic unpacking
    }
}

4 BigDecimal

BigDecimal: it is often used to solve the imprecise problem of accurate floating-point number operation

4.1 creating objects

Mode 1:
BigDecimal(double val)
Convert double to BigDecimal, which is the decimal representation of binary floating-point value of double. There is a pit!
Mode 2:
BigDecimal(String val)
Converts the form of a String of type String to BigDecimal

4.2 common methods

Add(BigDecimal bd): add
Subtract(BigDecimal bd): perform subtraction
Multiply(BigDecimal bd): perform multiplication
Divide(BigDecimal bd): perform a division operation. Exceptions will be thrown if the division is not complete
Divide(BigDecimal bd, reserved digits, rounding method): used when division is not complete
Setscale (reserved digits, rounding method): the same as above
pow(int n): find the power of data

4.3 exercise: Common test methods

package cn.api;

import java.math.BigDecimal;
import java.util.Scanner;

public class TestBigDecimal {
    public static void main(String[] args) {
        //f1();// The use of ordinary + - * / four arithmetic operations exposes the imprecision of floating-point arithmetic
        f2();//BigDecimal is used to solve the imprecise problem of floating-point operation
    }

    private static void f2() {
        //1. Prompt and receive two decimals entered by the user
        System.out.println("Please enter the two decimals you want to calculate:");
        double a = new Scanner(System.in).nextDouble();
        double b = new Scanner(System.in).nextDouble();
        //2. Create a tool class object and give the basic types A and b to the tool class object BigDecimal to save
        /*1.It's best not to use double as the parameter of the constructor, otherwise there will be imprecision and pit!!!*/
        /*2.It is best to use an overloaded constructor whose parameter type is String
        * double Turn to String and spell an empty String directly*/
        BigDecimal bd1 = new BigDecimal(a+"");//double + "" convert it to String
        BigDecimal bd2 = new BigDecimal(b+"");//double + "" convert it to String

        //3. Call its method through BigDecimal object to realize accurate operation
        //3.1 define the reference type variable of BigDecimal type to save the results
        BigDecimal bd3;
        //3.2 Add(BigDecimal bd): add
        bd3 = bd1.add(bd2);
                System.out.println("The result of addition is:"+bd3);
        //3.3 Subtract(BigDecimal bd): perform subtraction
        bd3 = bd1.subtract(bd2);
        System.out.println("The result of subtraction is:"+bd3);
        //3.4 Multiply(BigDecimal bd): perform multiplication
        bd3 = bd1.multiply(bd2);
        System.out.println("The result of multiplication is:"+bd3);
        //3.5 Divide(BigDecimal bd): perform a division operation, and throw an exception if the division is not complete
        /*3.In the division operation, an exception ArithmeticException will be thrown when the division is not complete*/
        //Scheme 1: (except that there are problems when it is not enough)
        //bd3 = bd1.divide(bd2);
        /*divide(m,n,o)
        m Is the object to be divided by, n refers to the number of digits to be retained, and o refers to the rounding method (such as rounding)*/
        //Scheme II:
        bd3 = bd1.divide(bd2,3,BigDecimal.ROUND_HALF_UP);
        System.out.println("The result of division is:"+bd3);

    }

    private static void f1() {
        //1. Prompt and receive two decimals entered by the user
        System.out.println("Please enter the two decimals you want to calculate:");
        double a = new Scanner(System.in).nextDouble();
        double b = new Scanner(System.in).nextDouble();

        //2. Do calculations
        System.out.println(a + b);//imprecise
        System.out.println(a - b);//imprecise
        System.out.println(a * b);//imprecise
        System.out.println(a / b);//imprecise
    }
}

5 expansion

Rounding method analysis
ROUND_HALF_UP is rounded, for example: 4.4, the result is 4; 4.5 the result is 5
ROUND_HALF_DOWN is rounded off and not rounded off. For example, 4.5 results in 4; 4.6 the result is 5
ROUND_HALF_EVEN fair rounding (commonly used by banks)
For example, between 5 and 6, close to 5 is discarded as 5, close to 6 is carried as 6, if it is 5.5, find an even number and become 6
ROUND_UP carries directly, not counting 0.1 or 0.9
ROUND_DOWN is discarded directly, not counting 0.1 or 0.9
ROUND_ Ceiling is rounded up to the greater of the actual value
In the direction of positive infinity, if round is a positive number, the behavior is the same as round_ Same as up, if it is negative, the behavior is the same as round_down like
ROUND_ Floor is rounded down to take the small value of the actual value
In the negative infinite direction, if round is a positive number, the behavior is the same as round_ Like down, if it is negative, the behavior is the same as round_ Same as up

Topics: Java regex