2016-11-16 51 views
0

所以我的程序知道文件的位置,它可以读取它有多少个单词,但是,我试图比较单词来计算单词的出现次数,与扫描仪一起使用。 该程序说,我不能将字符串转换为布尔值,我明白,但我会怎么做到这一点? 我可以得到一个答案,为什么它运行,但不会让我找到这个词来寻找 感谢我的程序从文件中读取,但无法找到文字

 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"); 
System.out.println("Enter the word you want to scan"); 
String fileName=scan.next().trim(); 
Scanner scr = new Scanner(new File(fileName)); 
// your code goes here ... 
while(scr.nextLine()){ 
    String word1 = scr.next(); 
    if (word1.equals(scr)){ 
     word++; 
    } 

} 
System.out.println("Total words = " + word); 

} 
} 
} 

回答

1

目前是否有可用的下一行,你只检查:

while(scr.hasNextLine()){ 

但你没有抓取它。它就像你永远留在文件中的相同位置。

要提取的下一行,你可以使用

scanner.nextLine()