I wanted "szSearch" to read the WORDLIST.txt file and check how many times the user's word appears in the file. So for example,
What word are you searching for? long
Searching the file...
The word long appears 24 times in the file WORDLIST.txt.
--- File End ---
public static void main(String[] args)
{
//creating scanner objects which will be used to read in the file
FileReader file = null;
BufferedReader br = null;
//declaring variables and assigning values
Scanner szKeyboard = new Scanner (System.in);
String szWord = "";
String szSearch;
int iCount = 0;
try
{
//open the file WORDLIST.txt using the Scanner and File classes
//File object used to open and store the file
//Scanner object will be used to read through the file object
file = new FileReader("WORDLIST.txt");
//needed for methods
br = new BufferedReader(file);
//ask the user what word they're searching for
System.out.print("What word are you searching for? ");
szWord = szKeyboard.nextLine();
System.out.println("Searching the file...");
szSearch = br.readLine();
//method to count how many times that word occurs in the WORDLIST.txt file
while (szSearch.contains(szWord))
{
iCount = iCount 1;
}
System.out.println("The word " szWord " appears " iCount " times in the file WORDLIST.txt.");
}//end try
catch (Exception e)
{
System.out.println("Error - Writing to File: " e);
}//end catch
finally
{
//close scanner
szKeyboard.close();
//finally runs regardless of wheter the try has worked
//the aim of a finally is to tidy up any loose ends
//if the contents within read is not equal to nothing i.e. it was possible to open and read
//close the file (try) if the file was not loaded catch the exception IOException
try
{
br.close();
`your text` }
catch(IOException e)
{
System.out.println("Error - Closing BufferReader: " e);
}
try
{
file.close();
}
catch(IOException e)
{
System.out.println("Error - closing FileReader: " e);
}
System.out.println("\n\n--- File End ---");
}//end try catch finally`
}//end class
This is what I tried doing but when I run it, it says:
What word are you searching for? long Searching the file... The word long appears 0 times in the file WORDLIST.txt.
--- File End ---
CodePudding user response:
Try replacing the while you have with this block of code, it probably requires some tweaking as I didn't test the code, but the logic I think solves it.
while ((strCurrentLine = objReader.readLine ())! = null) {
List<String> words = Arrays.asList(strCurrentLine.split(" "));
iCount = words.stream().hasMatch(w -> w.equals(szWord)).count();
}
CodePudding user response:
The issue in this code is that the while loop for counting the occurrences of the word the user is searching for is not correctly implemented. The loop only runs once and only checks if the current line contains the word, but it does not continue to check the next lines. Therefore, the count will always be either 0 or 1.
One fix could be:
while ((szSearch = br.readLine()) != null) {
if (szSearch.contains(szWord)) {
iCount ;
}
}
This way, the loop will continue to read the next line of the file as long as there are more lines to read. Within the loop, it checks if the current line contains the word the user is searching for, and if so, increments the count.
Another way to fix it is to use the String.split() method to split the line into words, then use a for loop to iterate over the words and check if the word matches the user input. This is most likely what you want. In the previous fix if the word comes up multiple times in a line it will only be counted once but this version will take that into account.
while ((szSearch = br.readLine()) != null) {
for (String word: szSearch.split(" ")) {
if (word.equals(szWord)) {
iCount ;
}
}
}
Here is the full code:
public static void main(String[] args) {
//creating scanner objects which will be used to read in the file
FileReader file = null;
BufferedReader br = null;
//declaring variables and assigning values
Scanner szKeyboard = new Scanner(System.in);
String szWord = "";
String szSearch;
int iCount = 0;
try {
//open the file WORDLIST.txt using the Scanner and File classes
//File object used to open and store the file
//Scanner object will be used to read through the file object
file = new FileReader("WORDLIST.txt");
//needed for methods
br = new BufferedReader(file);
//ask the user what word they're searching for
System.out.print("What word are you searching for? ");
szWord = szKeyboard.nextLine();
System.out.println("Searching the file...");
//method to count how many times that word occurs in the WORDLIST.txt file
while ((szSearch = br.readLine()) != null) {
for (String word: szSearch.split(" ")) {
if (word.equals(szWord)) {
iCount ;
}
}
}
System.out.println("The word " szWord " appears " iCount " times in the file WORDLIST.txt.");
} //end try
catch (Exception e) {
System.out.println("Error - Writing to File: " e);
} //end catch
finally {
//close scanner
szKeyboard.close();
//finally runs regardless of wheter the try has worked
//the aim of a finally is to tidy up any loose ends
//if the contents within read is not equal to nothing i.e. it was possible to open and read
//close the file (try) if the file was not loaded catch the exception IOException
try {
br.close();
} catch (IOException e) {
System.out.println("Error - Closing BufferReader: " e);
}
try {
file.close();
} catch (IOException e) {
System.out.println("Error - closing FileReader: " e);
}
System.out.println("\n\n--- File End ---");
} //end try catch finally
} //end class
Hope it helps.