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

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

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)

Liên kết rút gọn:

https://gokisoft.com/1057

Bình luận

avatar
Phạm Dương Minh An [java2_online]
2022-03-25 11:14:00
import java.util.Scanner;

public class Bai3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        System.out.print("Enter the number of contents: ");
        int n = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < n; i++) {
            System.out.print("Enter the content: ");
            sb.append(scanner.nextLine() + "-");
        }

        String[] strArr = sb.toString().split("-");
        for (String str : strArr) {
            if (str.contains("searching")) {
                System.out.println(str);
            }
        }
    }
}

avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:34:05



import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Bai3 {
    public static void main(String [] args){
        System.out.println("Nhap n phan tu");
        int n = (new Scanner(System.in)).nextInt();
        StringBuilder builder = new StringBuilder();
        for(int i =0; i<n;i++) {
            System.out.println(String.format("Chuoi thu %d",i));
            builder.append((new Scanner(System.in)).nextLine());
        }
        System.out.println("searching");
        String searching = (new Scanner(System.in)).nextLine();

        Pattern p = Pattern.compile(searching);
        Matcher matcher = p.matcher(builder);
        while(matcher.find()) {
            System.out.println(matcher.start() + "-" + matcher.end() + "-" + matcher.group());
        }

    }
}


avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:33:49



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

public class Bai2 {
    public static void main(String[] args) {
        System.out.println("Nhap n phan tu");
        List list = new ArrayList();
        int n = (new Scanner(System.in)).nextInt();
        for(int i=0;i<n;i++) {
            System.out.println(String.format("a[%d]",i));
            list.add((new Scanner(System.in)).nextInt());
        }
        System.out.println(list);
    }
}


avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:33:22

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Bai1 {
    public static void main(String [] args) {
        System.out.println("Enter source");
        String source = (new Scanner(System.in)).nextLine();
        System.out.println("Enter searching");
        String searching = (new Scanner(System.in)).nextLine();

        Pattern p = Pattern.compile(searching);
        Matcher matcher = p.matcher(source);
        int i =0;
        while (matcher.find()) {
            i++;
        }
        System.out.println(i);
    }
}


avatar
Đào Mạnh Dũng [C2010L]
2021-08-12 14:19:48

/*

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


import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Scanner;


/**

 *

 * @author Đào Dũng

 */

public class Gokisoft {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        //bai 1 

        Scanner scanner = new Scanner(System.in);

        String source = scanner.nextLine();

        String searching = scanner.nextLine();

        

        int c=0,index = 0;

        

        while (source.indexOf(searching, index)!=-1) {            

            index = 1+ source.indexOf(searching, index);

            c++;

        }

        System.out.println(c);

        //bai 2

        List<Integer> a1=new ArrayList<>();

        int n = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < n; i++) {

            a1.add(Integer.parseInt(scanner.nextLine()));

        }

        //bai 3

          

          LinkedList<StringBuilder> a = new LinkedList<>();

           n = Integer.parseInt(scanner.nextLine());

           searching = scanner.nextLine();

           for (int i = 0; i < n; i++) {

            a.add(new StringBuilder(scanner.nextLine()));

        }

           for (StringBuilder stringBuilder : a) {

               if (stringBuilder.indexOf(searching)!=-1) {

                   System.out.println(stringBuilder);

               }

        }

          

    }


}


avatar
Phạm Đăng Khoa [community,C2010L]
2021-08-12 14:14:46




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

import java.util.Scanner;

/**
 *
 * @author Khoa Pham
 */
public class Bai3 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("searching");
        Scanner input = new Scanner(System.in);
        int n;
        
        do{
            System.out.println("Nhap vao N chuoi tu ban phim: ");
            n = Integer.parseInt(input.nextLine());
            
        }while(n<1);
        System.out.println("Hien thi chuoi: ");
        for (int i = 0; i < n; i++) {
            System.out.println(sb);
        }
    }
}


avatar
Phạm Đăng Khoa [community,C2010L]
2021-08-12 14:02:09



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

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

/**
 *
 * @author Khoa Pham
 */
public class Bai2 {
    public static void main(String[] args) {
        List<Integer> sn = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        int n;
        while(true){
            System.out.println("Nhap vao N phan tu");
            n = Integer.parseInt(input.nextLine());
            if(n > 0){
                break;
            }
            System.err.println("Wrong type!");
        }
        for (int i = 0; i< n ; i++){
            System.out.println("Nhap vao so nguyen phan tu thu " +(i+1));
            sn.add(Integer.parseInt(input.nextLine()));
        }
        System.out.println("List cac so nguyen trong danh sach: ");
        for (Integer sn1 : sn){
            System.out.println(sn1+"\t");
        }
    }
    public static boolean checkNumber(String input){
        String regex = "[0-9]";
        return input.matches(regex);
    }
}


avatar
Phạm Đăng Khoa [community,C2010L]
2021-08-12 13:43:59



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

import java.util.Scanner;

/**
 *
 * @author Khoa Pham
 */
public class Bai1 {
    public static void main(String[] args) {
        String source,searching;
        int count = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao chuoi source");
        source = input.nextLine();
        System.out.println("Nhap vao chuoi searching");
        searching = input.nextLine();
        for (int i = 0; i < source.length(); i++) {
            if(source.charAt(i) == searching.charAt(0)){
                String temp = source.substring(i,i+searching.length());
                if(temp.equals(searching)){
                    count ++;
                }
            }
        }
        if (count != 0) {
            System.out.println("So lan xuat hien la: " +count);
        } else {
            System.out.println("Khong co");
        }
    }
}


avatar
Trần Thị Khánh Huyền [T2008A]
2021-03-12 02:59: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 javaadvance;

import java.util.ArrayList;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Bai2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList <Integer> arr;
        arr= new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int N;
        System.out.println("Nhap so nguyen N: ");
        N = sc.nextInt();
        for (int i=0; i<N;i++){
            System.out.println("Phan tu thu "+(i+1)+" la: ");
            arr.add(sc.nextInt());
        }
    }
    
}


avatar
Trần Thị Khánh Huyền [T2008A]
2021-03-12 02:58:51



/*
 * 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 javaadvance;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Bai1 {
    public static void main(String[] args) {
        String source, searching;
        int count=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap vao chuoi source: ");
        source = sc.nextLine();
        System.out.println("Nhap vao chuoi searching: ");
        searching = sc.nextLine();
        for(int i=0;i<source.length();i++){
            if(source.charAt(i)==searching.charAt(0)){
                String temp= source.substring(i,i+searching.length());
                if(temp.equals(searching)){
                  count ++;  
                }
            }
        }
        if (count!=0 ){System.out.println("So lan xuat hien la: "+count);
    }else{System.out.println("Khong co");}
}
}