By GokiSoft.com| 15:39 10/07/2023|
Java Advanced

Bài tập ôn luyện String, StringBuilder, StringBuffer - Java Advanced

Bài 1 :

Nhập vào chuỗi source, và chuỗi searching

Hiển thị ra số lần xuất hiện chuỗi searching trong chuỗi source.

Bài 2 :

Khái báo 1 mảng số nguyên sử dụng List. Nhập vào ngẫu nhiên N phần tử (N nhập từ bàn phím)

Bài 3 :

Nhập vào N chuỗi từ bàn phím và lưu vào StringBuilder. In ra tất cả các chuỗi mà có chứa chữ searching (được nhập từ bàn phím)

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

5

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

vuong huu phu [T2008A]
vuong huu phu

2021-03-11 09:28:44



/*
 * 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 javaapplication28;

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

/**
 *
 * @author Admin
 */
public class bai_3 {

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);
        ArrayList<StringBuilder> st = new ArrayList<>();
        System.out.println("Nhap so luong chuoi muon nhap ");
        n = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < n; i++) {

            System.out.format("Nhap phan tu thu [%d] = ", i + 1);
            String t = sc.nextLine();
            StringBuilder st1 = new StringBuilder(t);
            st.add(st1);
        }
        System.out.println("Tim kiem :");
        System.out.println("Nhap choi co ki tu la ");
        String a = sc.nextLine();
        for (int i = 0; i < st.size(); i++) {
            if (st.get(i).toString().equalsIgnoreCase(a)) {
                 System.out.format("Phan tu thu [%d] = %s", i + 1,st.get(i));
            }
        }
    }

}



vuong huu phu [T2008A]
vuong huu phu

2021-03-11 09:28:21



/*
 * 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 javaapplication28;

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

/**
 *
 * @author Admin
 */
public class Bai_2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int n ;
       Scanner scan = new Scanner(System.in);
      ArrayList<Integer> list = new ArrayList<>();
        System.out.println("Nhap so phan tu ");
        n = scan.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.format("Nhap phan tu thu [%d] = ", i+1 );
           int t = scan.nextInt();
            list.add(t);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.format("\nPhan tu thu [%d] =  %d ", i+1,list.get(i) );
        }
    }
    
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-03-11 02:46:49

#Bai1

#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 Java2.lesson1.OnLuyenString.bai1;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap source:");
        String source = scan.nextLine();
        System.out.println("Nhap searching:");
        String searching = scan.nextLine();
        int count =0;
        for (int i = 0; i < source.length(); i++) {
            if( source.indexOf(searching, i) != -1 ){
                i = source.indexOf(searching, i);
                count++;
            }
        }
        System.out.println("Searching xuat hien trong Source " + count + " lan");
    }
}

#Bai2

#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 Java2.lesson1.OnLuyenString.bai2;

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

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N:");
        int n = scan.nextInt();
        System.out.println("Nhap cac so:");
        for (int i = 0; i < n; i++) {
            int a = scan.nextInt();
            list.add(a);
        }
        for (int i = 0; i < n; i++) {
            System.out.print(list.get(i) + " ");
        }
    }
}

#Bai3

#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 Java2.lesson1.OnLuyenString.bai3;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        StringBuilder source = new StringBuilder();
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N:");
        int n = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap N chuoi:");
        for (int i = 0; i < n; i++) {
            String string = scan.nextLine();
            source.append(string);
        }
        System.out.println("Nhap chu muon tim kiem:");
        String search = scan.nextLine();
        for (int i = 0; i < source.length(); i++) {
            if(source.indexOf(search, i) != -1){
                System.out.println("String: " + source.substring(source.indexOf(search, i)));
                i = source.indexOf(search, i);
            }
        }
    }
}



trung [C1907L]
trung

2020-04-23 13:37:28



/*
 * 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 jv2;

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

/**
 *
 * @author prdox
 */
public class lession_1 {
//    public static void main(String[] args) {
//        Scanner inp = new Scanner(System.in);
//        System.out.println("Source : ?");
//        String sourceStr = inp.nextLine();
//        System.out.println("Searching : ?");
//        String searchStr = inp.nextLine();
//        String subStr = sourceStr;
//        int count = 0;
//        int index;
//        while (true) {
//            index = subStr.indexOf(searchStr);
//            if (index != -1){
//                count++;
//                subStr = subStr.substring(index+1);
//            } else{
//                break;
//            }
//        }
//        System.out.println("Number of occurence = " + count);
//    }
    
//    public static void main(String[] args) {
//        List<Integer> lst = new ArrayList<>();
//        Scanner inp = new Scanner(System.in);
//        System.out.println("Nhap vao n");
//        int n = inp.nextInt();
//        for (int i = 0; i < n; i++) {
//            System.out.println("Nhap vao so thu "+(i+1));
//            lst.add(inp.nextInt());
//        }
//    }
    
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao n");
        int n = Integer.parseInt(inp.nextLine());
        System.out.println("Nhap vao chuoi search");
        String searchStr = inp.nextLine();
        StringBuilder str = new StringBuilder();
        List<String> strContainsSearch = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap vao string thu "+ (i+1));
            String newStr = inp.nextLine();
            str.append(inp.nextLine());
            if (newStr.contains(searchStr)) strContainsSearch.add(newStr);
        }
        System.out.println("Cac chuoi co chua search String la ");
        strContainsSearch.forEach((string) -> {
            System.out.println(string);
        });
    }
}



hoangkhiem [C1907L]
hoangkhiem

2020-04-22 14:05:39

Bài 2 


/*
 * 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 Java2b1;

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

/**
 *
 * @author Admin
 */
public class bai1searchingsource {

    public static void main(String[] args) {
        Scanner nhap = new Scanner(System.in);
        List<Integer> arr;
        arr = new ArrayList<>();
        System.out.println("Mời bạn nhập n số nguyên : ");
        int n = Integer.parseInt(nhap.nextLine());
        if (n < 0) {
            System.out.println("MỜi nhập lại ");
        }
        for (int i = 0; i < n; i++) {
            System.out.format("arr[%d]= ", i + 1);
            arr.add(Integer.parseInt(nhap.nextLine()));
        }
        System.out.println("Các phần tử mảng là : ");
        for (Integer integer : arr) {
            System.out.println(integer);
        }
    }
}



hoangkhiem [C1907L]
hoangkhiem

2020-04-22 14:05:20


Bài 1
/*
 * 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 Java2b1;

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

/**
 *
 * @author Admin
 */
public class timchuoi {
    public static void main(String[] args) {

        Scanner nhap = new Scanner(System.in);
        String sourceStr ;
        String searchingStr ;
        int dem = 0;
        int demphu = 0;
        int indexStr = 0;
        List<Integer> VTC = new ArrayList<>();

        System.out.print("Nhập chuỗi ban đầu : ");
        sourceStr = nhap.nextLine();

        System.out.print("Nhập chuỗi tìm kiếm: ");
        searchingStr = nhap.nextLine();

        do{
            demphu++;
            //Truyền vào chuỗi con và chỉ số bắt đầu
            if (sourceStr.indexOf(searchingStr, indexStr) != -1) {
                //
                dem++;
                indexStr = sourceStr.indexOf(searchingStr, indexStr);
                //indexStr Cho kết quả trả về vào mảng
                VTC.add(indexStr);
                indexStr++;
            }
            //neu if không sảy ra dem không tăng và demphu không tăng và trả ra màn hình kết quả không tìm được
        } while (dem == demphu);

        System.out.println("KẾT QUẢ TÌM KIẾM");
        if (dem > 0) {
            System.out.println("Số lần chuỗi xuất hiện là: " + dem);
            for (int i = 0; i < VTC.size(); i++) {
                System.out.format("\nVị trí xuất hiện lần %d là: %d", i + 1, VTC.get(i));
            }
        } else {
            System.out.println("Kết quả không tìm được");
        }
    }
}



Hoàng Quang Huy [C1907L]
Hoàng Quang Huy

2020-04-22 14:03:36




package lesson1;

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

public class Main {

    public static void main(String[] args) {
        //Bài 1
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập vào chuỗi source: ");
        String sourcestr = scan.nextLine();
        System.out.println("Nhập vào chuỗi searching: ");
        String searchingstr = scan.nextLine();

        int count = 0;
        int index = 0;

        while ((index = sourcestr.indexOf(searchingstr, index)) != -1) {
            count++;
            index++;

        }
        System.out.println("số lần xuất hiện: " + count);
        //Bài 2
        System.out.println("Nhập số nguyên n:");
        try {
            int n = Integer.parseInt(scan.nextLine());
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                System.out.println("Nhập giá trị phần tử thứ " + (i + 1) + " : ");
                list.add(Integer.parseInt(scan.nextLine()));
            }
        } catch (NumberFormatException e) {
            System.out.println("Invalid Input");
        }
        //Bài 3
        List<StringBuilder> listbuilder = new ArrayList<>();

        System.out.println("Nhập N: ");
        int n3 = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n3; i++) {
            StringBuilder builder = new StringBuilder();
            builder.append(scan.nextLine());
            listbuilder.add(builder);
        }
        System.out.println("Nhập chuỗi cần tìm: ");
        String searching = scan.nextLine();
        for (StringBuilder str : listbuilder) {
            if (str.indexOf(searching) != -1) {
                System.out.println("String: " + str);
            }
        }
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-04-22 14:00:32



/*
 * 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 javaApril22;

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

/**
 *
 * @author Redmibook 14
 */
public class JavaApril22x {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter source string : ");
        String source = input.nextLine();
        System.out.println("Enter search string : ");
        String Search = input.nextLine();
        int index = 0;
        int count = 0;
        for (int i = 0; i < source.length(); i++) {
            if (source.indexOf(Search, index) != -1) {
                index = source.indexOf(Search, index);
                count++;
                index++;
            }

        }
        System.out.println(count);
        List<Integer> arr = new ArrayList<>();
        while (true) {
            System.out.println("Enter number : ");
            int number = Integer.parseInt(input.nextLine());
            arr.add(number);
            System.out.println("Continue Y/N ? ");
            String choice = input.nextLine();
            if (choice.equals("y")) {

            } else {
                break;
            }
        }

        System.out.println(arr.toString());
        StringBuilder Str = new StringBuilder();
        while (true) {
            System.out.println("Enter string : ");
            String number = input.nextLine();
            Str.append(number);
            System.out.println("Continue Y/N ? ");
            String choice = input.nextLine();
            if (choice.equals("y")) {

            } else {
                break;
            }
        }
        System.out.println(Str);
       
    }

}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-04-22 13:59:32



/*
 * 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 April22StringBuilder;

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

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Bai1: ");
        int count =0;
        System.out.print("Source: ");
        String source = input.nextLine();
        System.out.print("Searching: ");
        String searching = input.nextLine();
        for(int i=0;i<source.length();i++){
            if(source.charAt(i) == searching.charAt(0)){
                String tmp = source.substring(i,i+searching.length());
                if(tmp.equals(searching)){
                    count++;
                }
            }
        }
        if(count!=0) System.out.println(count);
        else{
            System.out.println("Cannot Found");
        }
        
        System.out.println("Bai2:");
        List<Integer> list = new ArrayList<>();
        System.out.print("Enter number of element: ");
        int element = Integer.parseInt(input.nextLine());
        for(int i=0;i<element;i++){
            System.out.print("Number: ");
            int tmp = Integer.parseInt(input.nextLine());
            list.add(tmp);
        }
        for (Integer integer : list) {
            System.out.println(integer);
        }
        
        System.out.println("Bai3:");
        StringBuilder build = new StringBuilder();
        System.out.print("Enter number of element: ");
        List<Integer> index = new ArrayList<>();
        int elemen = Integer.parseInt(input.nextLine());
        index.add(0);
        for(int i=0;i<elemen;i++){
            System.out.print("String: ");
            String tmp = input.nextLine();
            build.append(tmp);
            build.append(" ");
            index.add(build.length());
        }
        System.out.println("Inseart search String: ");
        String search = input.nextLine();
        int c =0;
        int vt=0;
        for(int i=0;i<build.length();i++){
            if(build.charAt(i) == search.charAt(0)){
                String tmp = build.substring(i,i+search.length());
                if(tmp.equals(search)){
                    System.out.println(build.substring(index.get(vt), index.get(vt+1)));
                    i=index.get(vt+1);
                    vt++;
                }
            }
        }
    }
}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-04-22 13:56:47



/*
 * 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 lession1;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Bai1 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        String source, searching;
        int count = 0;
        int lastCount = 0;
        int indexStr = 0;
        
        System.out.print("Nhập chuỗi source: ");
        source = scan.nextLine();
        System.out.print("Nhập chuỗi searching: ");
        searching = scan.nextLine();
        
        do{
            lastCount++;
            if(source.indexOf(searching, indexStr) != -1){
                count++;
                indexStr = source.indexOf(searching, indexStr);
                indexStr++;
            }
        }while(count == lastCount);
        
        if(count > 0){
            System.out.println("Số lần chuỗi xuất hiện là: " + count);
        }else{
            System.out.println("Chuỗi searchingStr không xuất hiện lần nào trong trong chuỗi sourceStr");
        }
    }
}



/*
 * 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 lession1;

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

/**
 *
 * @author ADMIN
 */
public class Bai2 {
    public static void main(String[] agrs){
        List<Integer> intArr = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int N;
        
        System.out.print("Nhập N: ");
        N = Integer.parseInt(scan.nextLine());
        if(N > 0){
            for(int i = 0; i < N; i++) {
                System.out.print("Nhập phần tử thứ " + (i + 1) + ": ");
                int temp = Integer.parseInt(scan.nextLine());
                intArr.add(temp);
            }
        }else {
            System.out.println("N phải lớn hơn 0!");
        }
        
    }
}



/*
 * 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 lession1;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Bai3 {
    public Bai3(String searching, Object par1) {
    }
    public static void main(String[] args){
        StringBuilder stringbuider = new StringBuilder("searching");
        Scanner scan = new Scanner(System.in);
        int N;
        System.out.print("Nhập vào N số chuỗi: ");
        N = Integer.parseInt(scan.nextLine());
        if(N > 0){
            System.out.println("Chuỗi hiển thị: ");
            for (int i = 0; i < N; i++) {
                System.out.println(stringbuider);
            }
        }    
    }
}