Help it is only printing the file name I need it to read thefile and pull the wo
ID: 3613578 • Letter: H
Question
Help it is only printing the file name I need it to read thefile and pull the words... also I need to check to see if the firstletter is a char "if not dont include the "word" " I know I need aloop but where and how to oly pull the first letter tocheck?????import java.util.*; import java.io.*;
public class CountOccurrenceOfWords { public static void main(String[] args) throwsFileNotFoundException { // Set text in a string Scanner keyboard = newScanner(System.in);
System.out.print("Enter a filename:: "); String fileName = keyboard.nextLine(); Scanner wordFile = new Scanner(newFileReader(fileName)); // Create a TreeMap to hold words as keyand count as value TreeMap<String, Integer> map = newTreeMap<String, Integer>(); while(wordFile.hasNext()){ String[] words =wordFile.split("[ .,';:!?(){}]"); error found Scanner RequiredString[] for (int i = 0; i < words.length; i++) { String key =words[i].toLowerCase();
if (words[i].length() > 0){ if (map.get(key) == null){ map.put(key, 1); } else { int value =map.get(key).intValue(); value++; map.put(key,value); } } }
// Get all entries into a set Set<Map.Entry<String, Integer>>entrySet = map.entrySet();
// Get key and value from each entry for (Map.Entry<String, Integer>entry: entrySet) System.out.println(entry.getValue()+ " " + entry.getKey()); } } } Help it is only printing the file name I need it to read thefile and pull the words... also I need to check to see if the firstletter is a char "if not dont include the "word" " I know I need aloop but where and how to oly pull the first letter tocheck?????
import java.util.*; import java.io.*;
public class CountOccurrenceOfWords { public static void main(String[] args) throwsFileNotFoundException { // Set text in a string Scanner keyboard = newScanner(System.in);
System.out.print("Enter a filename:: "); String fileName = keyboard.nextLine(); Scanner wordFile = new Scanner(newFileReader(fileName)); // Create a TreeMap to hold words as keyand count as value TreeMap<String, Integer> map = newTreeMap<String, Integer>(); while(wordFile.hasNext()){ String[] words =wordFile.split("[ .,';:!?(){}]"); error found Scanner RequiredString[] for (int i = 0; i < words.length; i++) { String key =words[i].toLowerCase();
if (words[i].length() > 0){ if (map.get(key) == null){ map.put(key, 1); } else { int value =map.get(key).intValue(); value++; map.put(key,value); } } }
// Get all entries into a set Set<Map.Entry<String, Integer>>entrySet = map.entrySet();
// Get key and value from each entry for (Map.Entry<String, Integer>entry: entrySet) System.out.println(entry.getValue()+ " " + entry.getKey()); } } }
Explanation / Answer
okay so tree map is not needed at all for this problem. you can have the buffered reader read the file for what you want. however I am confused. what are you checking for?? Are you looking for a particular char? or checking if the first word in the file is a char?? get back to me and I will solve..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.