ant c
ant c

Reputation: 83

my program reads from the file but cant find the words

So my program knows where the file is and it can read how many words it has, however, I am trying to compare words to count the occurrences of a word that i will use with a scanner. The program says i can't convert string to a boolean which i understand but how would i be able to make it happen? can I get an answer why it runs but doesn't allow me to find the word to look for thanks

       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);

}
}
}

Upvotes: 0

Views: 24

Answers (1)

Stefan Freitag
Stefan Freitag

Reputation: 3608

At present you are only checking if there is a next line available:

while(scr.hasNextLine()){

but you are not fetching it. Its like you are staying at the same position in the file forever.

To fetch the next line, you can make use of

scanner.nextLine()

Upvotes: 1

Related Questions