How to create a simple counter program?

Hints/suggestions/corrections for my Java program?

  • I need some serious help with constructing this program. I'm having a difficult time writing the codes out. I'm a Beginner in Java programming so please keep any hints/suggestions/corrections as simple as possible... ------------------------------- You will read one file to search for a given word and store a file with the search result. In addition, you are asked to print out the search result on the screen. For example, your program will take the following inputs from the keyboard. Enter a filename to read from: input.txt Enter a filename to write to: output.txt Enter a word to search: value Then, the following lines will be printed on the screen. Also, the same contents will be written to the output file (i.e., “output.txt” in this example). For your information, if m lines in the input file contain the search word, the screen and the input file will have m+2 lines. Therefore, just in case there is no line that contains the search word, only the last two lines will be printed on the screen and the output file. Line 1 contains the word “value” – number of occurrences = 1 Line 3 contains the word “value” – number of occurrences = 1 Line 6 contains the word “value” – number of occurrences = 3 … File “input.txt” contains 100 lines Number of occurrences of “value” = 33 (Requirement 1) The case in the search word and the file will be ignored. For example, the words will be considered identical: “VALUE”, “value”, “Value”, and so on. (Requirement 2) The search word can be either a substring or a whole string of an individual word in the input file. --------------------------------------... My program below: public class LucVincentSearchWord { // This method will read an input file line by line, then search for occurrences of the searchTerm public void searchWordInFile(File input, File output, String searchTerm) { System.out.println("What file to read from? "); System.out.println("What filename to write to? "); System.out.println("Enter a word to search: "); } public static void main(String[]args) throws IOException { // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Create a line counter variable int count = 0; Scanner input = new Scanner(); Scanner kbd = new Scanner(System.in); String line; String word; String searchTerm; String filename; // Get the filename. System.out.print("Enter the filename: "); filename = keyboard.nextLine(); // Open the input file PrintWriter outputFile = new PrintWriter(filename); // Open the output file System.out.println("What word you want to search for? "); word = kbd.nextLine(); while (input.hasNext()) { line = input.nextLine(); if (line.indexOf(word) >= 0) { System.out.println(line); count++; } } System.out.println(word + " occurs on " + count + " line(s) "); input.close(); // Close the input file // Close the output file } // This would contain your search and count logic and would return the count private int getWordOccurrenceCount(String inputLine, String searchTerm) { return ; } // Use whatever techniques you want to find occurrences of the search term and report a count back }

  • Answer:

    Okay, from the top. Your searchWordInFile method needs to do exactly what it says in the comment above it. It will SCAN the INPUT FILE line by line and search each line for the SEARCHTERM. How do you scanner the nextline in a File? Once you have the line, how can you tell if that String.contains the searchTerm? If it does contain the searchTerm, how can you count the number of times it occurs in that line? You have a getWordOccurenceCount method, it seems your teacher intends for you to call that method, passing to it the line you scanned in and the searchTerm. It will return the number of occurrences of the searchTerm in the line. Obviously it will return zero if the line does not contain the searchTerm. Now for your main method. This is where you will prompt the user, using the exact same wording as in the example, for the input file name, the output file name, and the search term. Look back to your searchWordInFile method, it is expect two File objects, so you will create new Files in the main method using the input from the user, NOT PrintWriter objects. You WILL NOT scan the files in the main method, use the searchWordInFile method for that. Lastly, why do you have three scanner objects in your main method? You only need one: to read the user's input. tl;dr - I'm not doing your homework for you, but I did give you way too many hints. Go back and actually read.

Vincent at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Okay, from the top. Your searchWordInFile method needs to do exactly what it says in the comment above it. It will SCAN the INPUT FILE line by line and search each line for the SEARCHTERM. How do you scanner the nextline in a File? Once you have the line, how can you tell if that String.contains the searchTerm? If it does contain the searchTerm, how can you count the number of times it occurs in that line? You have a getWordOccurenceCount method, it seems your teacher intends for you to call that method, passing to it the line you scanned in and the searchTerm. It will return the number of occurrences of the searchTerm in the line. Obviously it will return zero if the line does not contain the searchTerm. Now for your main method. This is where you will prompt the user, using the exact same wording as in the example, for the input file name, the output file name, and the search term. Look back to your searchWordInFile method, it is expect two File objects, so you will create new Files in the main method using the input from the user, NOT PrintWriter objects. You WILL NOT scan the files in the main method, use the searchWordInFile method for that. Lastly, why do you have three scanner objects in your main method? You only need one: to read the user's input. tl;dr - I'm not doing your homework for you, but I did give you way too many hints. Go back and actually read.

speedy35...

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.