我的程序不会要求用户输入文件中出现的内容
My program won't ask the user for an input for an occurrence in a file
所以我正在制作一个程序,它从我的文件中读取并允许用户输入一个词以查看该词的出现。
它运行正常,但不要求用户输入单词,我一直在想为什么,这是代码:
import java.util.*;
import java.io.*;
public class WordOccurence {
public static void main(String[] args) throws IOException {
int wordCount=0;
int word =0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter file name");
String fileName=scan.next().trim();
System.out.println("Enter the word you want to scan: ");
Scanner scr = new Scanner(new File(fileName));
// your code goes here ...
while(scr.hasNextInt()){
Scanner word2 = new Scanner(System.in);
String word1 = word2.next();
if (word1.equals(word2)){
word++;
}
}
System.out.println("Total words = " + word);
}
}
您的代码中几乎没有错误,这个版本应该可以按您的预期工作(我插入了评论试图解释如何修复)
public static void main(String[] args) throws IOException {
{
int word =0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter file name");
String fileName=scan.next().trim();
System.out.println("Enter the word you want to scan: ");
//You have to insert this line in order to ask the user to input the word to find
String wordTofind=scan.next().trim();
Scanner scr = new Scanner(new File(fileName));
// You should fix also here: scan the file and look if the word is present
while(scr.hasNext()){
String word1 = scr.next();
if (word1.equals(wordTofind)){
word++;
}
}
System.out.println("Total words = " + word);
}
}
别忘了关闭扫描仪:
scan.close();
scr.close();
如果你想让它询问用户,比如在弹出框中,你可以这样做:
String input = JOptionPane.showInputDialog("Your message");
编辑:您不需要扫描仪,除非您迭代输入。
所以我正在制作一个程序,它从我的文件中读取并允许用户输入一个词以查看该词的出现。 它运行正常,但不要求用户输入单词,我一直在想为什么,这是代码:
import java.util.*;
import java.io.*;
public class WordOccurence {
public static void main(String[] args) throws IOException {
int wordCount=0;
int word =0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter file name");
String fileName=scan.next().trim();
System.out.println("Enter the word you want to scan: ");
Scanner scr = new Scanner(new File(fileName));
// your code goes here ...
while(scr.hasNextInt()){
Scanner word2 = new Scanner(System.in);
String word1 = word2.next();
if (word1.equals(word2)){
word++;
}
}
System.out.println("Total words = " + word);
}
}
您的代码中几乎没有错误,这个版本应该可以按您的预期工作(我插入了评论试图解释如何修复)
public static void main(String[] args) throws IOException {
{
int word =0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter file name");
String fileName=scan.next().trim();
System.out.println("Enter the word you want to scan: ");
//You have to insert this line in order to ask the user to input the word to find
String wordTofind=scan.next().trim();
Scanner scr = new Scanner(new File(fileName));
// You should fix also here: scan the file and look if the word is present
while(scr.hasNext()){
String word1 = scr.next();
if (word1.equals(wordTofind)){
word++;
}
}
System.out.println("Total words = " + word);
}
}
别忘了关闭扫描仪:
scan.close();
scr.close();
如果你想让它询问用户,比如在弹出框中,你可以这样做:
String input = JOptionPane.showInputDialog("Your message");
编辑:您不需要扫描仪,除非您迭代输入。