I had to make a program that searches for a specific string, that the user inputs using a scanner, inside of a text file, and returns the number of times said string has been used in the text file. while trying to work it out in countless ways I somehow ended up with the program running but it asked me twice for the input and always only came back with "1". I worked at it so it has become a method but I'm not sure how to run this method since it does call on a text file and string of a users choice. This is what ive came up with so far:
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
public class WordCount
{
public static int countWord(File dataFile, String WordToSearch) throws FileNotFoundException
{
int count = 0;
dataFile = new File("text.txt");
Scanner FileInput = new Scanner(dataFile);
while (FileInput.hasNextLine())
{
Scanner s = new Scanner(System.in);
String search = s.next();
if (search.equals(WordToSearch))
count++;
}
return count;
}
}
This is the contents of the text file Im trying to call on "Hi Hello Goodbye Hi Hello Goodbye Hi Hello Goodbye Hi Hello Goodbye Hi Hello Goodbye Hi Hello Goodbye Hi Hello Goodbye"
please let me know if you spot any mistakes I might've missed in the code, Thank you so much for all the help.
Your problem is that if
you find a line that matches you break;
the loop. Because of this your counter will never exceed 1.
Instead you should remove the break;
from your code then the while
loop will run through all the lines.
EDIT:
The following code has been tested and works. Overall points worth noting are:
A. The main class is the first class that loads and therefore must run anything you want to run at the programs start. Also the main class must be static.
B. I have changed the Scanner
you were using to instead a BufferedReader
and a FileReader
these are different classes that are also designed to read files contents.
C. My while
loop only breaks;
when it reaches the end of the file. If you break
before then it will stop the search from finding all the occurrences.
D. I do not know exactly what you are trying to find. For example if you want to find entire lines that are equal to your word or entire lines that contain your word, etc. (if you tell me more specifically how you would like to refine your search i can update the code)
// When packaging you seriously reduce the size of your program if you don't import the entire java packages // here i have narrowed down the io* and util* to actually be the things you need
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ScannerInt {
public static void main(String[] args) {
// the main method is the first that is run.
// the main method MUST be static
// inside the main method put whatever you want to run in your program
scan(); // we run the scanner method which is created below
}
public static void scan() {
// first i create a scanner so that the java command prompt can take input
Scanner input = new Scanner(System.in);
System.out.println("Enter file path to file and file name: "); //prompt the reader to type the file directory and name
File file = new File(input.nextLine()); // determine what file they want based on that name
System.out.println("Enter the line you want to find: "); //prompt the reader to type the line they want to count
String line = input.nextLine(); // determine what line they want based on that string
// this statement tells them how many occurances there are
// this is done by the countWord() method (which when run returns a integer)
System.out.println("There are " + countWord(file, line) + " occurances.");
System.exit(0); // ends the program
}
// the countWord method takes a file name and word to search and returns an integer
public static int countWord(File dataFile, String WordToSearch) {
int count = 0; // start are counter
// NOTE: i prefer a BufferedReader to a Scanner you can change this if you want
// load the buffered reader
BufferedReader FileInput = null;
try {
// attempt to open the file which we have been told about
FileInput = new BufferedReader(new FileReader(dataFile));
} catch (FileNotFoundException ex) {
// if the file does not exist a FileNotFoundException will occur
Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex);
}
// then try searching the file
try {
// this while loop runs forever until it is broken below
while (true) {
String search = FileInput.readLine(); // we read a line and store it
if (search == null) { // if the line is "null" that means the file has ended and their are no more lines so break
break;
}
// then we check if this line has the text
// NOTE: i do not know what exactly you want to do here but currently this checks if the entire line
// is exactly equal to the string. This means though that if their are other words on the line it will not count
// Alternatively you can use search.contains(WordToSearch)
// that will check if the "WordToSearch" is anywhere in the line. Unfortunately that will not record if there is more than one
// (There is one more option but it is more complex but i will only mention it if you find the other two do not work)
if (search.equals(WordToSearch)) {
count++;
}
}
} catch (IOException ex) {
// if the line it tries to read does not exist a IOException will occur
Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex);
}
try {
FileInput.close(); // close the file reader
} catch (IOException ex) {
// if the FileInput reader has broken and therefore can not close a IOException will occur
Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex);
}
return count;
}