Hi,
I am having problems counting the number of words in a text file that i created (it has 130...input.txt) and printing that finding (number) into another text file (output)....Any help is welcomed. I have this, but it prints individual counts not a whole..and my other question is how would i print vowel/count them 2.?
iimport java.io.*; public class FileIO { public static void main(String[] args) { String filename = "G:\\Java 1302\\project\\FILEIO\\src\\input.txt"; // Relative pathname -- place in project directory String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt"; // String to hold the contents eventually try { FileReader fr = new FileReader(filename); BufferedReader br = new BufferedReader(fr); String lineRead = br.readLine(); while(lineRead != null){ fileContents = fileContents + "\n" + lineRead; lineRead = br.readLine(); } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); // Display the error System.exit(1); // Stop the program from running } catch (IOException e) { e.printStackTrace(); System.exit(1); } String[] wordList = fileContents.split("\\s"); int size = wordList.length; String [] words; int [] counts; words = new String [size]; counts = new int [size]; for(int i = 0; i < size; i++){ words[i] = wordList[i]; for(int j = 0; j < size; j++){ if(words[i].equals (wordList[j])){ counts[i] = counts[i] +1; } } System.out.println( " " + counts[i]); } String outputFilename = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt"; try { FileWriter fw = new FileWriter(outputFilename); BufferedWriter bw = new BufferedWriter(fw); // Use bw.write to write to the file for(int i = 1; i < size; i++){ bw.write(words[i] + ":" + counts[i] + "\n"); } bw.close(); // Always remember to close the file } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }