Some Assistance with Java IO?
-
After many attempts, I still can't get this correctly, any advice/help/corrections would be appreciated. Instructions: 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:
Here is the String API: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html Take a look at the String.indexOf(String,int) method. That's the one you want to use. You'll have to put it together with a bit of extra program logic, but I think you can figure that part out on your own.
Vincent at Yahoo! Answers Visit the source
Other answers
Here is the String API: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html Take a look at the String.indexOf(String,int) method. That's the one you want to use. You'll have to put it together with a bit of extra program logic, but I think you can figure that part out on your own.
green meklar
I guess you have asked for the same in your previous message This seems to be little more fine tuned than previous. Do spend your energies, I shall help you soon
James Bond
I guess you have asked for the same in your previous message This seems to be little more fine tuned than previous. Do spend your energies, I shall help you soon
James Bond
Related Q & A:
- How To Use Defense Assistance In Madden 2012?Best solution by ign.com
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Is there public assistance in North Carolina?Best solution by needhelppayingbills.com
- Does YMCA offer financial assistance?Best solution by Yahoo! Answers
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.