14th Day Notes

Source: Internet
Author: User
Tags character classes dateformat

Dark Horse programmer <a href= "http://www.itheima.com" target= "blank" >java training </a>

14th Day Notes

1. Overview of regular expressions and basic usage

Regular expressions are used to verify a string of characters that conform to the correct rules.

Introduction Rules:

There is a Matcher method in the string class that has a connection to a regular expression.

Package LM. Regex;

Import Java.util.Scanner;

/**
*
* @author Administrator
*
/**
* To verify the QQ number needs 1. length 5-15 number 2. Required 0 Cannot start
*
* Ideas: 1. Consider making a method return value: Boolean parameter: String to verify the QQ number
* 2. Enter a QQ number from the keyboard Scanner
* 3. Complete the function of the method checkqq
*
*/


public class RegexDemo2 {


public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter your QQ number:");
if (Method2 (Sc.nextline ()))
{
System.out.println ("Verification success");
}
Else
{
System.out.println ("Verification Failure");
}
}
/*
* The regular form of the certificate QQ
*/
private static Boolean Method2 (String s) {


return S.matches ("[1-9][0-9]{4,14}");


}

}

2. Constituent rule characters of regular expressions

x character X. Example: ' A ' denotes character a

\ \ backslash character.

\ n New Line (newline) character (' \u000a ')

\ r return character (' \u000d ')

Character class

[ABC] A, B or C (simple Class)

[^ABC] Any character except A, B, or C (negation)

[A-za-z] A to Z or A to Z, the letters at both ends are included (range)

[0-9] 0 to 9 characters are included

Predefined character classes

. Any character. And mine is. The character itself, how to express it? \.

\d number: [0-9]

\w Word character: [a-za-z_0-9]

Things that make up words in regular expressions must have these things.

Boundary Matching Device

^ The beginning of the line (Employment Class JS used)

End of the $ line (used in JS for employment Class)

\b Word boundaries

is the place where the word character is not.

Example: Hello World?haha;xixi

Greedy number of words

X? X, not once or once

X* X, 0 or more times

x+ X, one or more times

X{n} X, exactly n times

X{n,} X, at least n times

X{n,m} X, at least n times, but no more than m times

3. The judging function of regular expressions

In the String class

Public boolean matches (String regex)

Parameter: is regular expression (rule)

Return value: Represents whether the given rule matches.

4. Check case of mailbox

Regular:. matches any character.

Now to match "." \.

In Java, "\" requires the use of \ \

The \ in the regular. The Java code needs to be written in \ \.

 PackageLM. Regex;ImportJava.util.Scanner;/** Check email * [Email protected] * [email protected] * [email protected] * [EMAIL protected] * * Analysis: * 1. Define a method Checkemail * parameter: The mailbox return value to verify: compliance with rule * 2. Completing the Checkemail method function * 1. Define the rules for the mailbox * [0-9a-z_a-z]{3 , 10}@[0-9a-z_a-z]{2,5}\. [A-za-z] {2,3} **/ Public classRegexDemo3 { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter your email number:"); Boolean b=method2 (Sc.nextline ()); if(b) {System.out.println (b); System.out.println ("Validation succeeded"); } Else{System.out.println (b); System.out.println ("Verification Failure"); }    }    /** The regular form of the certificate QQ*/    Private StaticBoolean method2 (String s) {Boolean flag=false; String regex1= "[0-9a-z_a-z]{3,10}@[0-9a-z_a-z]{2,15}\\. [A-za-z] {2,3} "; String Regex2= "\\w{3,10}@\\w{2,10} (\\.\\w{2,3}) +"; if(S.matches (REGEX2)) {flag=true; }        returnFlag; }}

5. The segmentation function of regular expressions

Using the split method of the String class

Public string[] Split (String regex)

Parameters: Regular Expressions (rules);

Return value: An array, which is the information after the rule is cut.

 PackageLM. Regex;ImportJava.lang.reflect.Array;Importjava.util.Arrays;/** *  * @authorAdministrator * Split operation requirements string s= "AA,BB,CC"; to get ["AA", "BB", "CC"] * string ss = "aa.bb.cc"; * string S1 = "d:\\java0309\\day14-Common Object \\PPT"; */ Public classRegexDemo5 { Public Static voidMain (string[] args) {String s= "AA,BB,CC"; string[] Str=s.split (",");                        SYSTEM.OUT.PRINTLN (arrays.tostring (str)); String SS= "aa.bb.cc"; String[] str1=ss.split ("\ \"));                        System.out.println (arrays.tostring (str1)); String S1= "d:\\java0309\\day14-Common Object \\PPT"; String[] str2=s1.split ("\\\\");    System.out.println (arrays.tostring (str2)); }}

6.Math class overview and method usage

Java.lang.Math class It is an operation class about mathematical operations.

The method in it is static, using the direct Math. Method name.

 PackageLm.math;/*** Tags for Math class *@authorAdministrator **/ Public classMathDemo1 { Public Static voidMain (string[] args) {//1. Absolute valueSystem.out.println (Math.Abs (-10)); //2. RoundingSystem.out.println (Math.Round (15.9)); //3. Maximum and minimum values for two numbersSystem.out.println (Math.max (20, 10)); System.out.println (Math.min (20, 10)); //4. Open Square        intA= (int) Math.sqrt (9);                System.out.println (a); //5. Ask for a number of n-th square        intB= (int) Math.pow (10, 3);        System.out.println (b); //6. To find the downward value of a floating-point number upward value        Doublec=3.14; DoubleD=Math.floor (c); DoubleE=Math.ceil (c);        System.out.println (d);        System.out.println (e); //To find a random number        intRadom= (int) (Math.random () *100+1);            System.out.println (Radom); }}

7. How to get random numbers in any range

Get a random number between 50-100

int n= (int) (Math.random () *51+50);

(Math.random () * (100-50+1)) +50;

 PackageLm.math;/** *  * @authorAdministrator * Introduction to methods in the math class*/  Public classMathDemo2 { Public Static voidMain (string[] args) {////formula: Math.random (end-start+1) +start         intC= (int) (Math.random () *100) +1; System.out.println ("Random number of 1-100" +c); //50-100 does not contain         intD= (int) (Math.random () *50+50); System.out.println ("Random number of 50-100" +d); //51-100 does not contain         intA= (int) (Math.random () *50+51); System.out.println ("Random number of 51-100" +a); //get a number between 0-49                 intB= (int) (Math.random () *49); System.out.println ("Random number of 0-49" +b); //What---get is that 50-101 gets a random number between 50-100        intN= (int) (Math.random () *51+50) ; System.out.println ("Random number of 50-101" +N); }}

Class 8.random, which is a class that can fetch random numbers after jdk1.5.

Summarize:

Construction Method:

New Random () uses the millisecond value as the seed

The New random (long Seed) uses the specified value as the seed, and each time the resulting random number is the same

Member Methods:

Nextint (); Returns the random number of an int range

Nextint (n); Returns a random number within an N range

Overview and Construction methods for 9.Date

The date class requires mastery and is often used in employment classes.

Date under the Java.util package

The date class, which represents a point in time, is accurate to milliseconds.

10. DateFormat implementing the conversion of dates and strings to each other

DateFormat class It can convert between date----string.

Date---àstring formatting

String-àdate parsing

The DateFormat class is under the Java.text package.

In the actual development of the character and date are converted to each other, generally use the subclass of the DateFormat class

SimpleDateFormat

    1. How to format a Date object as a string of the specified effect

Use the Format method provided in the DateFormat class.

Public String Format (date date)

This method is also available in the SimpleDateFormat class.

Problem: SimpleDateFormat It has a parameterless construct, it can call the format method for formatting a Date object, but not get the effect we want, how to deal with it?

Notifies SimpleDateFormat that when it does the format operation, it is possible to get a string that we specify as an effect.

You can specify a template new SimpleDateFormat (String pattern) when you create SimpleDateFormat

 PackageLm.datefomat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;/*** * DateFormat A string that formats a date object into the specified effect*/ Public classDateFormDemo1 { Public Static voidMain (string[] args) {//Method OneDate d=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); String s=Sdf.format (d);                     System.out.println (s); //Method TwoSimpleDateFormat sdf1=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); String S1=Sdf.format (System.currenttimemillis ());               System.out.println (S1); }}

14th Day Notes

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.