Outline:
1. Regular expressions
2. Common exceptions
3. Inner class
------------------------------------------------------------------
Java formally supports regular expressions after JDK 1.4
Learning Goal: Only ask you to write some simple regular expressions on your own!
Complex we use third-party search!!!
Scenario: It is used to standardize the user input format!!!
Registration page
Please enter your username: (* The username starts with a lowercase letter and consists of a number of letters, not less than 6 bits in length)
int n = sc.nextint ();//It can only be used to accept data of type int
Once the user enters a string
Therefore, the user's input needs to be standardized!!! The character entered must be a numeric type character
---------------------------------------------------------------------------------------------
API:java.util.regex
Class
Pattern: Regular expression matching template class
Common methods:
1. Match a given regular expression to a pattern
Static Pattern compile (String regex);
2. Create a match for the given input with this pattern
Matcher Matcher (charsequence input)
3. Compile the given regular expression and try to match the given input to it
Static Boolean matches (String regex, charsequence input)
Java.util.regex
Class Matcher
Common methods:
1. Try to find the next subsequence of the input sequence that matches the pattern
Boolean find ()
2. Returns the initial index of the previous match
int start ()
int End ()
3. Returns the input subsequence that was matched by the previous match operation
String Group ()
Regular Expressions:
Predefined character classes
. Any character (may or may not match the line terminator)
\d number: [0-9]
\d non-numeric: [^0-9]
\s whitespace characters: [\t\n\x0b\f\r]
\s non-whitespace characters: [^\s]
\w Word character: [a-za-z_0-9]
\w non-word characters: [^\w]
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
--------------------------------------------------------------------------------
Abnormal:
The top-level data type of an object, exception: Java.lang.Throwable
According to the severity of the anomaly is divided into:
Error: The program is running with a very serious problem, once these problems occur
No need for manual processing by programmers!
Stackoverflowerror Stack Overflow
OutOfMemoryError Memory leaks
Exception (Exception): The program is running in the process of an abnormal situation!
It is divided into two main categories:
Run-time Exception: no exception detected
Inputmismatchexception is thrown by Scanner, indicating that the obtained token does not match the pattern of the desired type, or that the tag is outside the range of the expected type
IllegalArgumentException Illegal parameter exception
ArrayIndexOutOfBoundsException array subscript out of bounds exception
NullPointerException NULL pointer exception
ClassCastException Type Conversion Failure exception
Non-runtime exception: An exception was detected
ParseException parsing exceptions
Interruptedexception Interrupt exception
IOException exception
SQLException SQL statement exception
FileNotFoundException File not found exception
Note: 1. All runtimeexception run-time exceptions are subclasses of non-runtime exceptions!!!
2. All run-time exceptions are normally not required to be handled manually by the program!!!
Only need programmer normal coding carefully!!!
3. All non-runtime exceptions need to be handled manually by the programmer
The handling of non-runtime exceptions is divided into active treatment and negative treatment.
Positive treatment Method One:
Recommended: Different processing strategies can be made for different exceptions
try{
Statement1;
statement2;//If an exception occurs here, the code in the try block will no longer continue to execute down, but into
In the catch block corresponding to the exception type
Statement3;
Return
}catch (Exception type 1 exception object) {
Exception handling-Printing exception information
}catch (Exception type 2 exception object) {
Handling of exceptions
}catch (Exception type 3 exception object) {
///
}finally{
}
Note: 1. If there is a parent-child class relationship between the exception types in multiple catch blocks,
To put the subclass on top of the parent Class!!!
2.try...catch...finally use case, code in finally{}
Whether or not an exception occurs with try{}, the code in the finally will always execute unless
There are system.exit (0) in try;
3. You can do some finishing touches in the finally block! Resource shutdown, cache refresh
or the IO stream off and so on!!!
4. If there is a return statement in the try and the try{} code executes without any
Exception, is this finally{} code still executing?
A: Sure! When the program reads the return, it runs to the finally and executes
And then return to die!!!
Try not necessarily must be used with catch!!!
try{
....
}finally{
...
}
Positive treatment Method Two:
New features after the JDK7.0
try{
....
....
....
}catch (Exception Type 1 | Exception type 2 | Exception type 3 exception object name) {
...
}
Positive treatment Method Three:
try{
....
}catch (Total parent class (Exception) object) {
....
}
---------------------------------------------------------------------------
Negative handling:
1. If the exception thrown is a run-time exception, the method does not need to be passed
Throws Exception type
2. If the exception thrown in the method is a non-runtime exception
[In the method body, you need
Throw exception Object]
Also need to be on the method
Throws Exception type
Negative handling, no matter who the exception is thrown! Eventually there must be one to crawl
Unless this exception is thrown to the main method, once an exception occurs, then the JVM will stop!!!
When it comes to negative handling, the timing of an abnormal crawl
1. If a method B () throws an exception in XXX
If there is an exception xxx in another method A (), the exception that appears in B () is the same
A () will also go to call B ()
You can then choose to throw XXX exceptions in the B () method, and then crawl in a () uniform
--------------------------------------------------------------------------------
Custom Exceptions:
Write a class of your own and then inherit exception.
Add 5 construction Methods!!!
--------------------------------------------------------------------------------
Inner class:
Only need to be able to read!!!
1. member Inner class
The inner class as a member property in the outer class exists!
Note the point:
1. Internal classes can access all the properties in the external class
2. If an attribute in an inner class has the same name as an attribute in an external class
Then you need to access the internal through this. Property name!
3. Create an object: an external class. Inner class object name = External class object name. New inner Class ();
2. Static Inner class
The existence of an inner class as a static member property in an outer class
Note: 1 can only access static properties in external classes
3. Local inner class
4. Anonymous inner class
==============================================================================
Java Note 9-Regular expressions