How to create a simple counter program?

Hints/Suggestions/Corrections for this 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, it can Compile and Run with no trouble but I'm not getting the results I need: 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("Freind "); 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 file. outputFile.close(); System.out.println("Data written to the file.");// 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 2; } // Use whatever techniques you want to find occurrences of the search term and report a count back }

  • Answer:

    Just reread your code carefully, and you'll see that it does exactly what you tell it to do: 1) It reads from the string "Freind ", since the input scanner is created with new Scanner("Freind "); 2) It correctly searches for the word given on the command line and prints the number of results 3) It creates a file whose name is the one given on the command line, but never writes anything to outputFile, so the file will be empty If I search for "ei" for instance, which does appear in the string "Freind ", I get "ei occurs on 1 line(s)" Steps 1) and 3) are probably not what you wanted to do. Just get an input filename from the command line and create a file from it: Scanner input = new Scanner(new File(inputFilename)); And don't forget to write something to outputFile.

Vincent at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Just reread your code carefully, and you'll see that it does exactly what you tell it to do: 1) It reads from the string "Freind ", since the input scanner is created with new Scanner("Freind "); 2) It correctly searches for the word given on the command line and prints the number of results 3) It creates a file whose name is the one given on the command line, but never writes anything to outputFile, so the file will be empty If I search for "ei" for instance, which does appear in the string "Freind ", I get "ei occurs on 1 line(s)" Steps 1) and 3) are probably not what you wanted to do. Just get an input filename from the command line and create a file from it: Scanner input = new Scanner(new File(inputFilename)); And don't forget to write something to outputFile.

the dahu hunter

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.