I am trying to work out a problem involving finding the number of lines containing the word "hello" in an input file.
I have got the program to read the file from the correct place and the this count produces the correct number so long as hello features in each line which is wrong.
how would i go about producing the program so count produces the number of lines featuring the word "hello"
|
Code:
|
package assignment3;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FindInFile {
public static void main (String[] args) throws IOException
{
String line;
Scanner fileScan, lineScan;
int thisCount = 0;
fileScan = new Scanner (new File("C:/urls.inp.txt"));
// Read and process each line of the file
while (fileScan.hasNext())
{
line = fileScan.nextLine();
System.out.println ("Line: " + line);
lineScan = new Scanner (line);
lineScan.useDelimiter("hello");
while (lineScan.hasNext())
System.out.println (" " + lineScan.next());
thisCount++;
}
System.out.println("number of lines containing \"hello\" = " + thisCount);
}
} |