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)
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Phạm Dương Minh An [java2_online]](https://www.gravatar.com/avatar/ef937a51d2eb5de5d8542706812fa41c.jpg?s=80&d=mm&r=g)
Phạm Dương Minh An
2022-03-25 11:14:00
![Hieu Ngo [community,C2009G]](https://www.gravatar.com/avatar/cee2973101b9cf9580ef561ff3eeecf0.jpg?s=80&d=mm&r=g)
Hieu Ngo
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());
}
}
}
![Hieu Ngo [community,C2009G]](https://www.gravatar.com/avatar/cee2973101b9cf9580ef561ff3eeecf0.jpg?s=80&d=mm&r=g)
Hieu Ngo
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);
}
}
![Hieu Ngo [community,C2009G]](https://www.gravatar.com/avatar/cee2973101b9cf9580ef561ff3eeecf0.jpg?s=80&d=mm&r=g)
Hieu Ngo
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);
}
}
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
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);
}
}
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
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);
}
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
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);
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
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");
}
}
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
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());
}
}
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
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");}
}
}