Java 尝试从文件中读取并计算与数组中的单词匹配的短语数
我正在尝试读取用户提供的文件。程序应该扫描文件并计算文档中与我在数组中列出的单词相匹配的单词数。当我运行我的程序时,它会卡在我的while循环中,我不知道为什么会发生这种情况。有人能给我指出正确的方向吗Java 尝试从文件中读取并计算与数组中的单词匹配的短语数,java,file,while-loop,io,Java,File,While Loop,Io,我正在尝试读取用户提供的文件。程序应该扫描文件并计算文档中与我在数组中列出的单词相匹配的单词数。当我运行我的程序时,它会卡在我的while循环中,我不知道为什么会发生这种情况。有人能给我指出正确的方向吗 package pkgfinal.nhammer2; import java.nio.file.Files; import java.util.Scanner; import java.nio.file.Path; import java.nio.file.Paths; public c
package pkgfinal.nhammer2;
import java.nio.file.Files;
import java.util.Scanner;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FinalNhammer2 {
// Phishing Phrases
private static final String[] phishingPhrases = {"label", "invoice", "post",
"document", "postal", "calculations", "copy", "fedex", "statement",
"financial", "dhl", "usps", "8", "notification", "n", "irs", "ups",
"no", "delivery", "ticket", "express", "shipment", "international",
"parcel", "confirmation", "alert", "report", "idnotification", "shipping",
"ssn"
};
private static final int phrasePoints[] = {3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3,
2, 2, 3, 2, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
public static void main(String[] args) {
Scanner file = new Scanner(System.in);
int count = 0;
int numberOfPhrases = 30;
System.out.print("Enter file name: ");
Path path = Paths.get(file.nextLine());
if (Files.exists(path)) {
System.out.printf("%n%s exists%n", path.getFileName());
for (int i = 0; i < numberOfPhrases; i++){
while(file.hasNext()){
String word = file.next();
if (word.equalsIgnoreCase(phishingPhrases[i])){
count++;
}
}
}
System.out.printf("File has %d phishing phrases", count);
}
else{
System.out.printf("%s does not exist.", path);
}
}
}
package pkgfinal.nhammer2;
导入java.nio.file.Files;
导入java.util.Scanner;
导入java.nio.file.Path;
导入java.nio.file.path;
公开课财务2{
//网络钓鱼短语
私有静态最终字符串[]phishingPhrases={“标签”、“发票”、“帖子”,
“文件”、“邮寄”、“计算”、“副本”、“联邦快递”、“声明”,
“财务”、“dhl”、“usps”、“8”、“通知”、“n”、“irs”、“ups”,
“否”、“送货”、“客票”、“快递”、“装运”、“国际”,
“包裹”、“确认”、“警报”、“报告”、“idnotification”、“装运”,
“ssn”
};
私有静态final int phrasePoints[]={3,3,3,2,2,2,2,2,3,
2, 2, 3, 2, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
公共静态void main(字符串[]args){
扫描仪文件=新扫描仪(System.in);
整数计数=0;
int numberOfPhrases=30;
System.out.print(“输入文件名:”);
Path Path=Path.get(file.nextLine());
if(Files.exists(path)){
System.out.printf(“%n%s存在%n”,path.getFileName());
for(int i=0;i
解决代码中的以下问题:
扫描仪
int count = 0;
int numberOfPhrases = 30;
Scanner in = new Scanner(System.in);
System.out.print("Enter file name: ");
String fileName = in.nextLine();
File fileObj = new File(fileName);
if (fileObj.exists()) {
System.out.println("File exists");
Scanner file = new Scanner(fileObj);
while (file.hasNext()) {
String word = file.next();
for (int i = 0; i < phishingPhrases.length; i++) {
if (word.equalsIgnoreCase(phishingPhrases[i])) {
count++;
}
}
}
file.close();
System.out.printf("File has %d phishing phrases", count);
} else {
System.out.println("File does not exist");
}
int count=0;
int numberOfPhrases=30;
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入文件名:”);
字符串fileName=in.nextLine();
File fileObj=新文件(文件名);
如果(fileObj.exists()){
System.out.println(“文件存在”);
扫描仪文件=新扫描仪(fileObj);
while(file.hasNext()){
String word=file.next();
for(int i=0;i
我是否需要为文件fileObj=新文件(fileName)使用不同的导入;要工作吗?导入java.io.File
我在Scanner File=new Scanner(fileObj)行上遇到错误;错误是“未报告的异常FileNotFoundException;必须捕获或标记才能抛出”啊…只需在main
的声明中添加throws FileNotFoundException
,即public static void main(String[]args)throws FileNotFoundException
。如有任何进一步的疑问/问题,请随时发表评论。