arrays - How to get the last line of output from the loop for duplicate wordcount program in java? -
almost got output touchups needed.in code can total repeated words count text files through folder directory.now problem want output lines highlighted in images(attached ones). example had 2 text files in folder directory, 1st text file repeated word count 4 , 2nd text file repeated word count 31. present output shows count addition word word.
but want lastline (final count) output(you can see image attachment present output).the highlighted lines final line of each text files.i want omit remaining lines. so, output should
total words counted: 4 (text file 1) total words counted: 31(text file 2)
so can run 2000+text files folder output once.
i beginner java. :) code below:
package ramki; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.filenamefilter; import java.io.ioexception; import java.io.inputstreamreader; import org.apache.commons.io.fileutils; import org.apache.commons.io.ioutils; public class newrepeatedcount { public static void main(string[] args) { filenamefilter filter = new filenamefilter() { public boolean accept(file dir, string name) { return name.endswith(".txt"); } }; file folder = new file("e:\\testfolder\\"); file[] listoffiles = folder.listfiles(filter); (int = 0; < listoffiles.length; i++) { file file1 = listoffiles[i]; try { string content = fileutils.readfiletostring(file1); } catch (ioexception e) { e.printstacktrace(); } bufferedreader ins = null; try { ins = new bufferedreader(new inputstreamreader( new fileinputstream(file1))); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } string st = null; try { st = ioutils.tostring(ins); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } // split text array of words string[] words = st.split("\\s"); // frequency array int[] fr = new int[words.length]; // init frequency array (int i1 = 0; i1 < fr.length; i1++) fr[i1] = -1; // count words frequency (int i1 = 0; i1 < words.length; i1++) { (int j = 0; j < words.length; j++) { if (words[i1].equals(words[j])) { fr[i1]++; } } } // clean duplicates (int i1 = 0; i1 < words.length; i1++) { (int j = 0; j < words.length; j++) { if (words[i1].equals(words[j])) { if (i1 != j) words[i1] = ""; } } } // show output int total = 0; // system.out.println("duplicate words:"); (int i1 = 0; i1 < words.length; i1++) { if (words[i1] != "") { system.out.println(words[i1] + "=" + fr[i1]); total += fr[i1]; } system.out.println("total words counted: " + total); } } } }
here output screenshots
any suggestions welcomed.
just move last print statement out of loop , use file#getname()
file name.
for(int i1=0;i1<words.length;i1++){ if(words[i1]!=""){ //system.out.println(words[i1]+"="+fr[i1]);//just put in comments total+=fr[i1]; } } system.out.println("total words counted: "+total+" ( "+file1.getname() +" )");
output:
total words counted: 3 ( file1.txt ) total words counted: 3 ( file2.txt )
Comments
Post a Comment