Ok im having a problem with my code I\'m trying to get myprogram to read a file
ID: 3613515 • Letter: O
Question
Ok im having a problem with my code I'm trying to get myprogram to read a file called README.txt and the program can notfind it!! where should the file be saved so it can find it. Insteadof having it read only the file set in the code how do I get it toto take a file that I type in? ex through scanner?package exercise22_9;
import java.util.*; // Provides TreeMap, Iterator,Scanner import java.io.*; // Provides FileReader,FileNotFoundException
public class WordCount {
public static void main(String[]args) { TreeMap<String, Integer> frequencyData = newTreeMap<String, Integer>( ); readWordFile(frequencyData); printAllCounts(frequencyData);
}
public static int getCount(String word, TreeMap<String,Integer> frequencyData) { if (frequencyData.containsKey(word)) { // The word has occurred before, so get its count from themap return frequencyData.get(word); // Auto-unboxed } else { // No occurrences of this word return 0; } }
public static void printAllCounts(TreeMap<String,Integer> frequencyData) { System.out.println("-----------------------------------------------"); System.out.println(" Occurrences Word");
for(String word : frequencyData.keySet( )) { System.out.printf("%15d %s ", frequencyData.get(word),word); }
System.out.println("-----------------------------------------------"); }
public static void readWordFile(TreeMap<String, Integer>frequencyData) { Scanner wordFile; String word; // A word read from the file Integer count; // The number of occurrences of the word
try { wordFile = new Scanner(new FileReader("README.txt")); } catch (FileNotFoundException e) { System.err.println(e); return; }
while (wordFile.hasNext()) { // Read the next word and get rid of the end-of-line marker ifneeded: word = wordFile.next();
// Get the current count of this word, add one, and then storethe new count: count = getCount(word, frequencyData) + 1; frequencyData.put(word, count); } } } Ok im having a problem with my code I'm trying to get myprogram to read a file called README.txt and the program can notfind it!! where should the file be saved so it can find it. Insteadof having it read only the file set in the code how do I get it toto take a file that I type in? ex through scanner?
package exercise22_9;
import java.util.*; // Provides TreeMap, Iterator,Scanner import java.io.*; // Provides FileReader,FileNotFoundException
public class WordCount {
public static void main(String[]args) { TreeMap<String, Integer> frequencyData = newTreeMap<String, Integer>( ); readWordFile(frequencyData); printAllCounts(frequencyData);
}
public static int getCount(String word, TreeMap<String,Integer> frequencyData) { if (frequencyData.containsKey(word)) { // The word has occurred before, so get its count from themap return frequencyData.get(word); // Auto-unboxed } else { // No occurrences of this word return 0; } }
public static void printAllCounts(TreeMap<String,Integer> frequencyData) { System.out.println("-----------------------------------------------"); System.out.println(" Occurrences Word");
for(String word : frequencyData.keySet( )) { System.out.printf("%15d %s ", frequencyData.get(word),word); }
System.out.println("-----------------------------------------------"); }
public static void readWordFile(TreeMap<String, Integer>frequencyData) { Scanner wordFile; String word; // A word read from the file Integer count; // The number of occurrences of the word
try { wordFile = new Scanner(new FileReader("README.txt")); } catch (FileNotFoundException e) { System.err.println(e); return; }
while (wordFile.hasNext()) { // Read the next word and get rid of the end-of-line marker ifneeded: word = wordFile.next();
// Get the current count of this word, add one, and then storethe new count: count = getCount(word, frequencyData) + 1; frequencyData.put(word, count); } } }
Explanation / Answer
In response to your PM, if you want to input afile name through keyboard input: // keyboardinput Scanner keyboard = new Scanner(System.in); // prompt for user input out.print("Enter a filename:: "); String filename = file.nextLine(); With filename, you can now create a newFile, as shown below, or use that asthe parameter for your FileReader. File file =new File(filename);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.