alice.java
来自「java 语言实现查找alice个数 呷阿根阿哥」· Java 代码 · 共 45 行
JAVA
45 行
import java.io.*;
import java.util.regex.*;
import javax.swing.JOptionPane;
public class alice {
public static void main(String[] args) throws IOException{
String fileName;
String filePath;
filePath=JOptionPane.showInputDialog("请输出文件的路径:");
fileName=JOptionPane.showInputDialog("请输出文件的名字:");
File name=new File(filePath, fileName);
try{
BufferedReader buf = new BufferedReader(new FileReader(name));
StringBuffer sbuf = new StringBuffer();//缓冲字符串
String line = null;
while((line = buf.readLine())!=null){
sbuf.append(line);//追加到缓冲字符串中
}
buf.close();//读取结束
String str = sbuf.toString();
Pattern p = Pattern.compile("Alice");//定义正则表达式匹配单词
Matcher m = p.matcher(str);
/* StringBuffer sb = new StringBuffer();*/
int i=0;
boolean result = m.find();
while(result) {
i++;
result = m.find();
}
System.out.println(i);
JOptionPane.showMessageDialog(null , "Alice的数目是:"+i, "结果", JOptionPane.PLAIN_MESSAGE);
}
catch(IOException ioEception){
JOptionPane.showMessageDialog(null , "文件不存在或路径错误或文件没有带上后缀", "错误", JOptionPane.PLAIN_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?