Java常用類練習(下篇)

B:空指針異常

C:數組下標越界異常

package xie.i;

publicclass Test5 {

publicstaticvoid main(String[] args) {

String []s=new String[10];

try{

s[11]="5";

}catch(Exception e){

System.out.println("數組下標越界!");

}finally{

System.out.println(3);

}

}

}

D:定義時異常

package xie.i;

publicclass Test6 {

publicstaticvoid main(String[] args) {

try{

int[]a=newint[-2];

}catch(Exception e){

System.out.println("定義時出現異常");

}

}

}

E:IO異常

try{

FileInputStream fis=new FileInputStream("f:/13.doc");

}catch(FileNotFoundException e){

System.out.println("找不到文件");

}

}

}

:自定義異常類

package xie.i;

publicclass Test7 {

publicstaticvoid main (String[]args){

B b=new B();

try{

b.f();

}catch(TestqException e){

e.printStackTrace();

}

}

}

classTestqExceptionextends Exception {}

class B{

publicvoid f() throws TestqException{

System.out.println(123);

}

}

異常關鍵字用法注意點:

package xie.i;

publicclass Test3 {

publicvoid f() throws Exception{

thrownew Exception("不能爲零!");

//throw一定拋出異常,在拋出異常後,後面的語句就不被執行;

}

publicstaticvoid main(String[] args){

    Test3 t=new Test3();

try {

t.f();

catch (Exception e) {//一個try裏面可以有多個catch語句,一個try裏面可以再有try

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

練習(1):請輸入一個數,範圍在14,如果不滿足,就請重新輸入(用正則表達式方法編寫)

方法()

package xie.i;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

publicclass Test10 {

publicstaticvoid main(String[] args) {

Scanner s=new Scanner(System.in);

System.out.println("請輸入數字:");

String a=s.nextLine();

Pattern p=Pattern.compile("[0-4]");

Matcher m=p.matcher(a);

boolean b=m.matches();//試着嘗試整個去區匹配

while(!b){

System.out.println("請重新輸入:");

a=s.nextLine();

m=p.matcher(a);

b=m.matches();

}

}

}

方法():

package xie.i;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

publicclass Test10 {

publicstaticvoid main(String[] args) {

Scanner s=new Scanner(System.in);

while(true){

System.out.println("請輸入數字:");

String a=s.nextLine();

Pattern p=Pattern.compile("[0-4]");

Matcher m=p.matcher(a);

if(m.find()){

break;

}else{

System.out.println("請重新輸入:");

}

}

}

}

練習(2):

已知字符串:123,,,.....**//789aaa;

(1)請用正則表達式輸出字符串中的符號;

(2)請用正則表達式輸出字符串的數字和字母;

第一小題:

package xie.i;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

publicclass Test11 {

publicstaticvoid main(String[] args) {

String s="123,,,....**//789aaa";

Pattern p=Pattern.compile("\\p{Punct}");

Matcher m=p.matcher(s);

while(m.find()){

System.out.print(m.group()+" ");

}

}

}

第二小題:

package xie.i;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

publicclass Test11 {

publicstaticvoid main(String[] args) {

String s="123,,,....**//789aaa";

Pattern p=Pattern.compile("\\p{Alnum}");

Matcher m=p.matcher(s);

while(m.find()){

System.out.print(m.group()+" ");

}

}

}

練習():

輸出大前天的時間;精確定幾分幾秒星期幾?

package xie.i;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

publicclass QianT {

publicstaticvoid main(String[] args) {

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");

System.out.println(sdf.format(new Date()));

Calendar c=Calendar.getInstance();

c.add(c.DAY_OF_MONTH,-3);

System.out.println(c.get(c.YEAR)+"年"+(c.get(c.MONTH)+1)+"月"+c.get(c.DAY_OF_MONTH)+"日");

   System.out.println(sdf.format(c.getTime()));

}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章