public class IsSubStringDemo { public static boolean isSubString(String str, String subStr) { int strLen = str.length(); int subStrLen = subStr.length(); if (strLen < subStrLen) { return false; } for (int i = 0; i <= strLen - subStrLen; i++) { if (str.substring(i, i + subStrLen).equals(subStr)) { return true; } } return false; } } import java.io.*; public class CountThe { public static void main(String[] args) throws IOException { File file = new File("zuowen.txt"); // 请替换成你的文件路径 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); StringBuffer buffer = new StringBuffer(); String line; int count = 0; while ((line = reader.readLine()) != null) { buffer.append(line); } String text = buffer.toString(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '的') { count++; } } System.out.println("文档中共有" + count + "个“的”字。"); } } 怎样更改才能运行
时间: 2024-02-14 09:17:02 浏览: 22
你需要将两个类的代码合并到同一个文件中,像这样:
```
import java.io.*;
public class IsSubStringDemo {
public static boolean isSubString(String str, String subStr) {
int strLen = str.length();
int subStrLen = subStr.length();
if (strLen < subStrLen) {
return false;
}
for (int i = 0; i <= strLen - subStrLen; i++) {
if (str.substring(i, i + subStrLen).equals(subStr)) {
return true;
}
}
return false;
}
public static void main(String[] args) throws IOException {
File file = new File("zuowen.txt"); // 请替换成你的文件路径
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
StringBuffer buffer = new StringBuffer();
String line;
int count = 0;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String text = buffer.toString();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '的') {
count++;
}
}
System.out.println("文档中共有" + count + "个“的”字。");
}
}
```
然后运行这个文件即可。