First I have my text file called "input.txt"
For example, the file contains the following:
Hi
Hello
Bye
HELLO
GoodBye
Hi
hello
I am trying to write a java program that first asks the user to enter a word (any word), read as a String. The program should keep asking for a word until the user enters -1 (also read as a String).
Once a word has been entered, my program must go in the file input.txt and count the number of times the word appears (this could be zero – 0 if the word does not appear in the file). You must go through the entire file to get a complete count. Once I know the final count for a given word, output the results in a file using the following format (as an example):
Monitor 2
Comparisons between words inputted from the user and words in Lab7.txt should be case insensitive. However, words printed in the output file should be the same as originally entered by users (i.e., case sensitive).
P.S. I must use only two loops to complete this lab. One main loop that keeps
asking the user for a word and prints the results in the output file. Another loop, within the main loop, to go through the file Lab7.txt and keep track of the count. I will have to reset the cursor in the file Lab7.txt before reading a new word from the user. To do this, close the file and re-open it again.
Example:
Please enter a keyword (enter -1 to exit): Hi
Please enter a keyword (enter -1 to exit): Hello
Please enter a keyword (enter -1 to exit): Good
Please enter a keyword (enter -1 to exit): HELLO
Please enter a keyword (enter -1 to exit): -1
Output.txt:
Hi 2
Hello 2
Good 0
HELLO 1
The following is my code but Then I get stuck after I ask the user to enter a keyword...
import java.io.PrintStream;
import java.util.Scanner;
import java.lang.String;
import java.io.File;
public class readAFile{
public static void main(String[] args)throws java.io.IOException{
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
Scanner fileInput = new Scanner(new File("input.txt"));
PrintStream fileOutput = new PrintStream(new File("output.txt"));
int i = -1;
String exit = i + "";;
int count = 0;
System.out.print("Please enter a keyword (enter -1 to exit): ");
for (String word = input.nextLine(); !word.equals(exit); word = input.nextLine()){
System.out.print("Please enter a keyword (enter -1 to exit): ");
for (int x = 0; fileInput.hasNextLine(); x++){
}
}
Bookmarks