By GokiSoft.com| 19:24 01/04/2020|
Java Basic

Share Code - Tìm kiếm chuỗi trong chuỗi + Exception

1. Hướng dẫn chữa bài tập

Tìm kiếm chuỗi trong chuỗi



/*
 * 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 lession7.string;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String sourceStr;
        String searchingStr;
        System.out.println("Nhap chuoi nguon: ");
        sourceStr = scan.nextLine();
        System.out.println("Nhap chuoi tim kiem: ");
        searchingStr = scan.nextLine();
        //tim ra so lan xuat hien searchingStr trong sourceStr & index
        //indexOf >= 0 -> index xuat hien cua searchingStr trong source
//        int index = sourceStr.indexOf(searchingStr);
//        System.out.println("index: " + index);
        //Cach 1: ko toi uu => for tu dau toi cuoi
        int count = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < sourceStr.length(); i++) {
            int index = sourceStr.indexOf(searchingStr, i);
            if(index >= 0 && !list.contains(index)) {
                count++;
                list.add(index);
                System.out.println("index: " + index);
            }
        }
        System.out.println("Count: " + count);
        //Cach 2 : Toi uu source & performent
        System.out.println("======= Cach 2 ============");
        count = 0;
        for (int i = 0; i < sourceStr.length();) {
            int index = sourceStr.indexOf(searchingStr, i);
            if(index >= 0) {
                count++;
                i = index + 1;
                System.out.println("index: " + index);
            } else {
                break;
            }
        }
        System.out.println("Count: " + count);
    }
}

Phần 2: Exception



/*
 * 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 lession7.string;

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();
        
        int k;
        try {
            k = Calculator.devide(6, 0);
            System.out.println("k = " + k);
        } catch (CalculatorException ex) {
            System.out.println(ex.getMessage());
            //Trong TH cua cac ban => nghiep vu quy
        }
        
//        if(y == 0) {
//            System.out.println("Phep chia cho 0");
//        } else {
//            int result = x/y;
//            System.out.println("result: " + result);
//        }
        int[] t = null;
        try {
            int result = x/y;
            System.out.println("result: " + result);
            
            t = new int[2];
            //length: 2, index: 0-1
            t[2] = 10;
            System.out.println("t[2] = " + t[2]);
        } catch(ArithmeticException e) {
//            e.printStackTrace();
            System.out.println("Chia cho 0");
        } catch(ArrayIndexOutOfBoundsException e) {
//            e.printStackTrace();
            System.out.println("index out of bound exception");
        } finally {
            //huy du lieu
            scan = null;
            x = 0;
            y = 0;
            t = null;
            System.out.println("Huy data thanh cong");
        }
    }
}



/*
 * 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 lession7.string;

/**
 *
 * @author Diep.Tran
 */
public class CalculatorException extends Exception{
    int x, y;

    public CalculatorException(String message, int x, int y) {
        super(message);
        this.x = x;
        this.y = y;
    }

    @Override
    public String getMessage() {
        return "x / y = " + x + "/" + y + ", " + super.getMessage();
    }
}



/*
 * 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 lession7.string;

/**
 *
 * @author Diep.Tran
 */
public class Calculator {
    public static int devide(int x, int y) throws CalculatorException {
        if(y == 0) {
            //tra ve du lieu la bao nhieu
            //tao ra 1 crash => thong bao cho lap trinh vien khac hieu
            throw new CalculatorException("Devide by Zero", x, y);
        } else {
            return x/y;
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)