By GokiSoft.com|
09:50 31/08/2021|
Java Advanced
[Share Code] Hướng dẫn tìm hiểu Exception + StringBuilder + StringBuffer + Pattern & Matcher - Lập trình Java nâng cao
#TestPackage.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson01;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Diep.Tran
*/
public class TestPackage {
public static void main(String[] args) {
String s = "A";
s += "B";
s += "C";
s += "D";
s += "E";
System.out.println("s = " + s);
//Trong bai toan -> string -> join -> large
StringBuilder builder = new StringBuilder();
builder.append("123123");
builder.append("sdfdsf");
builder.append("srewr23");
builder.append("234asd");
builder.append("t43t3t");
System.out.println(builder.toString());
StringBuffer buffer = new StringBuffer();
buffer.append("123123");
buffer.append("sdfdsf");
buffer.append("srewr23");
buffer.append("234asd");
buffer.append("t43t3t");
System.out.println(buffer.toString());
String s1 = "Sinh vien Aptech123 abcs aptech323 okk aptech666 akak";
//Tim kiem xem trong chuoi s1 co chua bao nhieu chuoi tuan theo quy tac sau
//ab -> a:[a-zA-Z]{3,10}, b:[0-9]{2,6}
//Code nhu the nao??? for, if, else, ... => giai thuat.
String s2 = "[a-zA-Z]{3,10}[0-9]{2,6}";
Pattern p = Pattern.compile(s2);
Matcher matcher = p.matcher(s1);
while(matcher.find()) {
System.out.println(matcher.start() + "-" + matcher.end() + "-" + matcher.group());
}
}
}
#Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson01;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x=");
int x = scan.nextInt();
System.out.println("Nhap y=");
int y = scan.nextInt();
float s;
try {
s = Calculator.chia(x, y);
System.out.println("s = " + s);
} catch (Exception ex) {
//Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
}
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson01;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//Exception
//TH1 -> Giai phap -> Fix logic -> Chinh xac
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x=");
int x = scan.nextInt();
System.out.println("Nhap y=");
int y = scan.nextInt();
//Cach 1 -> Fix logic
if(y == 0) {
System.out.println("Khong thuc hien phep chia cho 0");
} else {
int s = x/y;
System.out.println("s = " + s);
}
//TH -> goi thu vien ben thu 3.
try {
//De khoi code -> ghi co the gay ra error
int s = x/y;
System.out.println("s = " + s);
} catch(ArithmeticException e) {
//Xay ra error -> nhay vao khu vuc nay
System.out.println("ArithmeticException Exception ...");
} finally {
//Du code error - hay ko error -> deu goi vao day
//Code dat trong finally -> giai phong tai nguyen
System.out.println("finally ...");
}
//TH2
int[] t = {1, 5, 2};//length: 3, index: 0 -> 2
for (int i = 0; i < t.length; i++) {
System.out.printf("\nt[%d]=%d", i, t[i]);
}
}
}
#Calculator.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson01;
/**
*
* @author Diep.Tran
*/
public class Calculator {
public static float cong(float x, float y) {
return x + y;
}
public static float tru(float x, float y) {
return x - y;
}
public static float nhan(float x, float y) {
return x/y;
}
/**
* Yeu cau nghiep vu -> TH y = 0 -> chuong trinh thong bao error -> Crash
* @param x
* @param y
* @return
*/
public static float chia(float x, float y) throws Exception {
if(y == 0) {
throw new Exception("Error chia cho 0");
}
return x/y;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)