[零基础学JAVA]Java SE应用部分-22.Eclipse及正则表达式使用
版权声明:自创著作,谢绝转发!不然将追究法律责任。本季主要知识点:
Eclipse开发工具开发JAVA程序
正则表达式的使用
Eclipse:本来的含义是:日蚀
JAVA是SUN企业开发的。Eclipse最早是由IBM开发的,以后转送给了eclipse组织,如今是一个完全免费的开发工具,可以直接从eclipse.org下载到最新的版本。下载完以后直接解压缩即可运行。
在eclipse中一个工作区中可以有多个JAVA的项目。
Eclipse自身支持以下几个功能:
·JDT:JAVA的开发环境
·Junit:测试环境
·CVS客户端:
·Eclipse插件开发
可以直接选择新建JAVA项目
新建一个类
JAVA的命名规则:
·类称号:每个单词的首字母大写
·方法称号:第一个单词的首字母小写,以后每个单词的首字母大写
·属性称号:第一个单词的首字母小写,以后每个单词的首字母大写
·常量称号:每个单词的字母大写
·包称号:一切单词的字母小写
调整字体大小
显示行号
测试打印HelloWorld
在eclipse中只需一保留文件,就可以自动进行编译
再新建一个Person类进行简单测试哈~
类写好了以后需求写属性,属性写好以后需求写setter和getter方法,这时可以使用生成代码功能
下面生动生成结构方法
下面我们再写一个Math类
Ctrl+Shift+F:可以格式化代码
下面的Demo01中调用此Math类
代码下有红线划出说明代码有问题,提示UnhandledexceptiontypeException,可以使用Ctrl+1:纠正代码功能。
我们选择try/catch
运行测试下
下面看下eclipse的调试功能哈
双击加断点
问是不是要切换到debug界面,选择yes表明切换。
选择单步过滤
单步越过:只看成果,不看代码内部的操作
单步跳入:进入代码内部看运算成果。
假如直接选择单步路过,我们将直接看到成果2
下面我们把Math类代码写稍微复杂点哈
持续Debug这个Demo01
选择单步跳入
如今进入到结构方法,持续单步跳入
这儿进入Math方法内,观察其值的改变,i的值为10,j的值为5
x的初值为0
returnx;时,x的值为2
我们可以使用debug功能跟踪第行代码、每个值的改变哈~
在eclipse中还包括了Junit测试工具,此工具是一个开源项目,也是一组jar包。我们需求手艺将这些jar包增加进来。
如今我们来测试Math类中的div方法
TestCase:表明一个测试用例
TestSuite:表明多个测试用例一起进行测试。
在eclipse中虽然现已集成了junit,但是要把包手艺的加进来。
此jar包位于eclipse\plugins中
手艺增加哈~
增加扩展jar
增加完扩展jar包后程序没有红线了哈~说明这个包准确了哈~
下面我们进行测试哈,使用TestCase.assertEquals()来测试div()方法,这就表明测试了
TestCase.assertEquals(newMath().div(10,5),2);
这儿有个异常我们需求进行异常捕获下
运行junit测试
在Junit中有两个著名的标志:
·GreenBar:表明测试成果准确
·RedBar:表明测试成果不准确
我们验证下10/5=6看下作用
假如我们设定成果有个差错,夸大点,差错为4
此刻10/2=6就通过测试哈~
正则表达式:
从JDK1.4开始在JAVA集成,最早都使用Apache的一套组件。通常使用正则进行数据的验证。主要是用于字符串的。
在String类中有以下三个方法集成了正则的使用:
·publicbooleanmatches(Stringregex);--gt;匹配的
·publicStringreplaceAll();
·publicStringsplit();--gt;拆分
请求一:
请求验证一个字符串是不是是由数字构成的。假如是则显示true,不然显示false
我们使用正则表达式来搞定哈~
如何表明数字,假如不认识,我们可以检测str.matches()方法
测试一下
如今匹配的是一位字符串,假如想匹配多位,假定此字符串的长度为6~15位。
验证成功哈~~~
请求二:
验证一个字符串是不是是由字母构成的(a~z、A~Z),长度也是6~15位
字符串里面再加此数字验证下
这即是正则表达式基本操作哈~
请求三:
验证一个字符串是不是不是以数字构成的。假定字符串长度依然是6~15位。
假如第一位不能是数字
请求四:
给一个字符串,请求依照数字拆分,即:一切的数字都是拆分点,只需是有数字就要拆分,所以此处可以跟上一个长度的通配符:“+”
通配符有以下几个:
·
:表明长度为0位、1位或多位
·+:表明长度为1位或多位
·?:表明长度为1位或0位
实际上用于很多的程序来讲,部分时候是不太习气直接去写之类的代码,而是喜欢用一些别的的内容进行代替:
·\d:表明数字
·\D:表明非数字
·\w:表明由字母、数字、下划线构成
·\W:表明由非字母、数字、下划线构成
请求五:
请求判别一个字符串是不是是由字母、数字、下划线构成
请求六:
Replace方法也支持正则,例如:将一个字符串中的数字全面替换成“X”。
正则表达式在程序开发中常用于拆分字符串及匹配,比方网站常常要对输入的email地址进行验证。
请求七:
对email地址进行验证
总结
1、eclipse使用
2、正则表达式的使用(匹配、拆分)
#######################################################################
(Copyright statement: self-created works, please refuse to forward! Otherwise, legal responsibility will be pursued.
Main points of knowledge this season:
Eclipse development tool to develop JAVA program
Use of regular expressions
Eclipse: The original meaning is: solar eclipse
JAVA is developed by SUN enterprise. Eclipse was first developed by IBM and later transferred to the eclipse organization. Now it is a completely free development tool that can be downloaded directly from eclipse.org to the latest version. After downloading, unzip it and run it.
There can be multiple JAVA projects in a workspace in eclipse.
Eclipse itself supports the following features:
·JDT: JAVA development environment
Junit: test environment
·CVS client:
Eclipse plug-in development
You can directly choose to create a new JAVA project
create a new class
JAVA naming rules:
Class Title: Capitalize the first letter of each word
Method name: lowercase the first letter of the first word, capitalize the first letter of each subsequent word
Attribute name: the first letter of the first word is lowercase, and the first letter of each subsequent word is uppercase
Constant title: capital letters for each word
·Package name: lowercase letters of all words
Adjust font size
show line number
Test print HelloWorld
In eclipse, you can compile automatically as soon as you keep the file
Create a new Person class for a simple test~
After the class is written, you need to write properties, and after the properties are written, you need to write setter and getter methods. At this time, you can use the function of generating code
The following vivid structure method
Next we write another Math class
Ctrl Shift F: can format code
This Math class is called in the following Demo01
There is a red line under the code to indicate that there is a problem with the code, prompting UnhandledceptiontypeException, you can use Ctrl 1: Correct the code function.
We choose try/catch
run the test
Let's take a look at the debugging function of eclipse
Double click to add breakpoint
Ask if you want to switch to the debug interface, select yes to indicate the switch.
Choose single step filter
Step over: only look at the results, not the operation inside the code
Step into: Enter the code to see the result of the operation.
If we directly choose to step through, we will directly see the result 2
Let's make the Math class code a little more complicated.
Continue to debug this Demo01
Select to step into
Now into the structure method, keep stepping in
Here, enter the Math method and observe the change of its value. The value of i is 10 and the value of j is 5.
The initial value of x is 0
When returnx;, the value of x is 2
We can use the debug function to track the change of the first line of code and each value~
Also included in eclipse is the Junit test tool, which is an open source project and a set of jar packages. We need technology to add these jar packages.
Now let's test the div method in the Math class
TestCase: Indicates a test case
TestSuite: Indicates that multiple test cases are tested together.
Although junit has been integrated in eclipse, the package technology should be added.
This jar package is located in eclipse\plugins
Craftsmanship increases~
Add extension jar
After adding the extension jar package, there is no red line in the program~ It means that this package is accurate~
Let's test it, using TestCase.assertEquals() to test the div() method, which indicates that the test
TestCase.assertEquals(newMath().div(10,5),2);
There is an exception here, we need to catch the exception
run junit tests
There are two well-known flags in Junit:
GreenBar: Indicates that the test results are accurate
· RedBar: Indicates that the test results are inaccurate
Let's verify the effect of 10/5=6
If we set the result to have an error, exaggerate it, the error is 4
At this moment, 10/2=6 will pass the test~
Regular expression:
Since JDK1.4, it has been integrated in JAVA, and a set of Apache components has been used at the earliest. Data is usually validated using regular expressions. Mainly used for strings.
There are three methods in the String class that integrate regular usage:
·publicbooleanmatches(Stringregex);--gt;matched
·publicStringreplaceAll();
·publicStringsplit();--gt;split
Request one:
Request to verify that a string is composed of numbers. If so, display true, otherwise display false
We use regular expressions to do it~
How to indicate the number, if we don't know it, we can detect the str.matches() method
have a test
Now the match is a one-bit string. If you want to match multiple bits, it is assumed that the length of this string is 6~15 bits.
Verification is successful~
Request two:
Verify that a string is composed of letters (a~z, A~Z), and the length is also 6~15 digits
Add this number to the string to verify
This is the basic operation of regular expressions~
Request three:
Verify that a string is not composed of numbers. Assume that the string length is still 6~15 bits.
If the first digit cannot be a number
Request four:
Give a string and request to split according to the number, that is: all numbers are split points, as long as there are numbers, they will be split, so here you can follow a wildcard of length: " "
Wildcards are as follows:
·
: indicates that the length is 0, 1 or more digits
: indicates that the length is 1 or more digits
?: Indicates the length is 1 or 0 digits
In fact, it is used in many programs, and sometimes it is not used to write such code directly, but prefer to replace it with some other content:
\d: indicates a number
·\D: indicates non-digital
·\w: Indicates that it consists of letters, numbers, and underscores
·\W: Indicates that it consists of non-letters, numbers, and underscores
Request five:
Request to determine whether a string is composed of letters, numbers, and underscores
Request Six:
The Replace method also supports regular expressions, for example: replace all numbers in a string with "X".
Regular expressions are often used to split strings and match in program development. For example, websites often need to verify the entered email address.
Request Seven:
Verify email address
Summarize
1. Using eclipse
2. The use of regular expressions (matching, splitting)
################################################## ####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################)
页:
[1]