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)

Phạm Ngọc Minh [T1907A]
Phạm Ngọc Minh

2020-04-15 07:20:50



package B1_java2;

import java.util.Scanner;


public class main {
        public static void main (String[]args){
        String source;   
        int searching = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap  vao chuoi bat ky di my fen: ");
        source = scan.nextLine();
        
        for (int i = 0; i < source.length(); i++) {
            
                searching ++;
            
            }
        System.out.println("So lan xuat hien cua chuoi searching " +  " = " + searching);
    }
}



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

public class List {
     public static void main(String[] args) {
       List<Integer> soNguyen;
        soNguyen = new ArrayList<>();
        int n;
        Scanner scanner = new Scanner(System.in);
        
        do{
            System.out.println("Nhap N so nguyen:");
            n = scanner.nextInt();
        }while(n < 0);
        System.out.println("Moi ban nhap cac so nguyen: ");
        System.out.println("============================");
        for(int i=0;i<n;i++){
            System.out.println("Nhap so nguyen thu : " +i);
            soNguyen.add(scanner.nextInt());
        }
    }
}



package B1_java2;
import java.util.Scanner;

public class StringBuilder {

    public StringBuilder(String searching, Object par1) {
    }
    public static void main(String[] args){
        StringBuilder stringbuider = new StringBuilder("searching");
        Scanner scanner = new Scanner(System.in);
        int n;
        
        do{
            System.out.println("Nhap vao N so chuoi : ");
            n = scanner.nextInt();
        }while(n < 1);
        System.out.println("Hien thi chuoi: ");
        for (int i = 0; i < n; i++) {
            System.out.println(stringbuider);
        }        
    
    }

}



nguyễn văn huy [T1907A]
nguyễn văn huy

2020-03-24 14:28:16



/*
 * 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 StringBuilderStringBuffer;
import java.util.*;
/**a
 *
 * @author ASUS
 */
public class list {
    public static void main(String[] args) {
        ArrayList<Integer> inter=new ArrayList<>();
        Scanner scan=new Scanner(System.in);
        int n = 0;
        
        do{
            System.out.println("nhap n:");
            n = Integer.parseInt(scan.nextLine());
        }while(n>0);
        System.out.println("nhap");
        for (int i = 0; i < n; i++) {
            inter.add(scan.nextInt());
        }
    }
}
/////
/*
 * 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 StringBuilderStringBuffer;

import java.util.*;

/**
 *
 * @author ASUS
 */
public class xuathien {

    public static void main(String[] args) {
        String soure;
        int searching = 1;
        Scanner scan = new Scanner(System.in);
        System.out.println("nhap chuoi soure:");
        soure = scan.nextLine();
        for (int i = 0; i < soure.length(); i++) {
            searching++;
        }
        System.out.println("so lan xuat hien" + searching);
    }

}



NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-03-22 20:07:27



/*
 * 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 hashmap;
import java.util.Scanner;
/**
 *
 * @author abc
 */
public class BtString {
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap vao 1 string");
        String string = scan.nextLine();
        System.out.println("Nhap vao searching");
        String searching = scan.nextLine();
        
        System.out.println(count(string,searching));
        
      
        
        
    }
    
    public static int count(String string , String searching)
    {
        int res = 0;
        int a = string.length();
        int b = searching.length();
        
        for ( int i = 0 ; i <= a-b ; i ++)
        {
            int j ;
            
            for ( j = 0 ; j < a ; j++)
            {
                if(searching.charAt(i+j)!= string.charAt(j))
                {
                    break;
                }
            }
            
            if( j == a)
            {
                res++;
                j = 0;
            }
        }
        return res ;
    }
}


/*
 * 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 hashmap;
import java.util.Scanner;
import java.util.ArrayList;
/**
 *
 * @author abc
 */
public class List {
    public static void main(String[] args)
    {
        ArrayList<Integer> integer = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int N;
        N = Integer.parseInt(scan.nextLine());
        
        for ( int i = 0 ; i < N ; i++)
        {
           int n = scan.nextInt();
           integer.add(n);
        }
        
        for (int i = 0 ; i < integer.size(); i++)
        {
            System.out.println(integer.get(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 hashmap;
import java.util.Scanner;
import java.util.ArrayList;
/**
 *
 * @author abc
 */
public class String3 {
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<String> string = new ArrayList<>();
        int N;
        String a;
        N = scan.nextInt();
        
        for(int i = 0 ; i < N+1 ; i++)
        {
            a = scan.nextLine();
            string.add(a);
        }
        
        for ( int i = 0 ; i < string.size(); i++)
        {
            
            if(string.get(i).contains("searching")== true)
            {
                System.out.println(string.get(i));
            }
        }
        
    }
    
}




Đường Thanh Bình [T1907A]
Đường Thanh Bình

2020-03-22 15:58:12

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 B1_javaAd;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class main {
        public static void main (String[]args){
        String source;   
        int searching = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap  vao chuoi bat ky di my fen: ");
        source = scan.nextLine();
        
        for (int i = 0; i < source.length(); i++) {
            
                searching ++;
            
            }
        System.out.println("So lan xuat hien cua chuoi searching " +  " = " + searching);
    }
}
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 B1_javaAd;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class List {
     public static void main(String[] args) {
       List<Integer> soNguyen;
        soNguyen = new ArrayList<>();
        int n;
        Scanner scanner = new Scanner(System.in);
        
        do{
            System.out.println("Nhap N so nguyen:");
            n = scanner.nextInt();
        }while(n < 0);
        System.out.println("Moi ban nhap cac so nguyen: ");
        System.out.println("============================");
        for(int i=0;i<n;i++){
            System.out.println("Nhap so nguyen thu : " +i);
            soNguyen.add(scanner.nextInt());
        }
    }
}
bài 3


/*
 * 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 B1_javaAd;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class StringBuilder {

    public StringBuilder(String searching, Object par1) {
    }
    public static void main(String[] args){
        StringBuilder stringbuider = new StringBuilder("searching");
        Scanner scanner = new Scanner(System.in);
        int n;
        
        do{
            System.out.println("Nhap vao N so chuoi : ");
            n = scanner.nextInt();
        }while(n < 1);
        System.out.println("Hien thi chuoi: ");
        for (int i = 0; i < n; i++) {
            System.out.println(stringbuider);
        }        
    
    }

}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-22 14:55:37


bai2
package java2_Advanced.BaiTapNgay20_3_2020.OnLuyen_String;

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

public class Bai2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        System.out.println("Nhap n so muon them: ");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap so thu " + (i + 1) + " :");
            list.add(scan.nextInt());
        }

        for (Integer number : list) {
            System.out.println("Danh sach cac so vua nhap: " + number);
        }
    }
}


bai3
package java2_Advanced.BaiTapNgay20_3_2020.OnLuyen_String;


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

public class Bai3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<StringBuilder> stringBuilders = new ArrayList<>();

        System.out.println("Nhap vap so chuoi: ");
        int n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap chuoi thu " + (i + 1) + " :");
            StringBuilder string = new StringBuilder(scanner.nextLine());
            stringBuilders.add(string);
        }

        for (StringBuilder abc : stringBuilders) {
            if (abc.toString().contains("searching")) {
                System.out.println(abc);
            }
        }
    }
}



Phí Văn Long [T1907A]
Phí Văn Long

2020-03-21 12:29:08

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 java2.Lesson1;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Source {
    public static void main (String[]args){
        String source;   
        int searching = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap  vao chuoi bat ky di my fen: ");
        source = scan.nextLine();
        
        for (int i = 0; i < source.length(); i++) {
            
                searching ++;
            
                }
        
        System.out.println("So lan xuat hien cua chuoi searching " +  " = " + searching);
    }
}

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 java2.Lesson1;

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

/**
 *
 * @author Admin
 */
public class List {
   public static void main(String[] args) {
        List<Integer> soNguyen;
        soNguyen = new ArrayList<>();
        int n;
        Scanner scanner = new Scanner(System.in);
        
        do{
            System.out.println("Nhap N so nguyen:");
            n = scanner.nextInt();
        }while(n < 0);
        System.out.println("Moi ban nhap cac so nguyen: ");
        System.out.println("============================");
        for(int i=0;i<n;i++){
            System.out.println("Nhap so nguyen thu : " +i);
            soNguyen.add(scanner.nextInt());
        }
    }
}

bài 3:



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

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Bai3 {
     public static void main(String[] args) {
        StringBuilder stringbuider = new StringBuilder("searching");
        Scanner scanner = new Scanner(System.in);
        int n;
        
        do{
            System.out.println("Nhap vao N so chuoi : ");
            n = scanner.nextInt();
        }while(n < 1);
        System.out.println("Hien thi chuoi: ");
        for (int i = 0; i < n; i++) {
            System.out.println(stringbuider);
        }        
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-03-21 08:09:35



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

import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author DELL
 */
public class bt1 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("nhap chuoi : ");

        StringTokenizer st = new StringTokenizer(scan.nextLine());
        int count = 0;
        while (st.hasMoreTokens()) {
            if (st.nextToken().equalsIgnoreCase("searching")) {
                count++;
            }
        }
        System.out.println("so lan searching xuat hien la " + count);
    }
}



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

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

/**
 *
 * @author DELL
 */
public class bt2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Integer> songuyen;
        songuyen = new ArrayList<>();
        while (true) {            
            System.out.println("add : ");
            int a;
            a= Integer.parseInt(scan.nextLine());
            songuyen.add(a);
            System.out.println("End !! --> enter '2020'");
            int b = songuyen.size()-1;
            
            if (a==2020)  {
                songuyen.remove(b);
                break;
            }
            
        }
        for (int i = 0; i < songuyen.size(); i++) {
            System.out.println(""+songuyen.get(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 Practice;

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

/**
 *
 * @author DELL
 */
public class bt3 {
  public static void main(String[] args) {
        List<StringBuilder> strbd = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int n;
        System.out.println("so chuoi muon nhap : ");
        n= Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Chuoi thu " + (i+1));
            String str;
            str = scan.nextLine();
          StringBuilder sb = new StringBuilder(str);
          strbd.add(sb);
      }
        
        for (StringBuilder stringBuilder : strbd) {
            if (stringBuilder.toString().contains("searching")) {
                System.out.println(stringBuilder);
            }
      }
    }
}



Minh Nghia [T1907A]
Minh Nghia

2020-03-21 02:42:18



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

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

/**
 *
 * @author Administrator
 */
public class Bai2 {
    public static void main(String[] args) {
        List<Integer> songuyen = new ArrayList<>();
        int n;
        Scanner scan = new Scanner(System.in);
        
        do{
            System.out.println("Nhap N so nguyen:");
            n = scan.nextInt();
        }while(n < 0);
        System.out.println("Nhap cac so nguyen:");
        for(int i=0;i<n;i++){
            System.out.println("Nhap so nguyen thu :" +i);
            songuyen.add(scan.nextInt());
        }
    }
    
    
}



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

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Bai3 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("searching");
        Scanner scan = new Scanner(System.in);
        int n;
        
        do{
            System.out.println("Nhap vao N so chuoi :");
            n = scan.nextInt();
        }while(n < 1);
        System.out.println("Hien thi chuoi:");
        for (int i = 0; i < n; i++) {
            System.out.println(sb);
        }        
    }
}



thienphu [T1907A]
thienphu

2020-03-21 01:03:20



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

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

/**
 *
 * @author Thien Phu
 */
public class ListSoNguyen {

    public static void main(String[] args) {
        List<Integer> songuyen = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        int n;
        while (true) {
            System.out.println("Nhan N so nguyen vao danh sách:");
            n = Integer.parseInt(sc.nextLine());
            if (n > 0) {
                break;
            }
            System.err.println("Nhap sai ròi");
        }
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap so nguyen thứ " + (i + 1));
            songuyen.add(sc.nextInt());
        }
        System.out.println("Danh sach cac số nguyên trong danh sách");
        for (Integer songuyen1 : songuyen) {
            System.out.print(songuyen1+"\t");
        }

    }

    public static boolean checkNumber(String input) {
        String regex = "[0-9]";
        return input.matches(regex);
    }

}



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

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

/**
 *
 * @author Thien Phu
 */
public class BT3 {

    public static void main(String[] args) {
        List<StringBuilder> chuoi = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap N chuoi muon nhap");
        int n = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Nhập chuỗi thứ " + (i + 1));
            StringBuilder sb = new StringBuilder(sc.nextLine());
            chuoi.add(sb);
        }
        System.out.println("Các chuỗi có chứa searching");
        for (StringBuilder chuoi1 : chuoi) {
            if (chuoi1.toString().contains("searching")) {
                System.out.println(chuoi1);
            }
        }
    }
}