Monday 15 August 2011

list - orderedlinkedlist JAVA counting words in .txt file no duplicate words plus counter> -



list - orderedlinkedlist JAVA counting words in .txt file no duplicate words plus counter> -

okay programme supposed count number of times each word shows in .txt file i'm on right track because programme shows every word in file in text area need not repeat words , increment counter instead. on right track under each word:words statement? it's in ordered linked list words in order alphabetically...

import javax.swing.*; import java.awt.*; import java.io.*; import java.util.scanner; public class oct29 extends jpanel { private orderedlist<string> words; private string filename; private int width = 800; private int height = 600; private textarea textarea; public oct29() { scanner scan; textarea = new textarea("",0,0,textarea.scrollbars_vertical_only); textarea.setfont(new font("helvetica",font.plain,24)); textarea.setpreferredsize(new dimension(width,height)); setpreferredsize(new dimension(width,height)); add(textarea); jfilechooser chooser = new jfilechooser("../text"); int returnvalue = chooser.showopendialog(null); if(returnvalue == jfilechooser.approve_option) { seek { file file = chooser.getselectedfile(); filename = file.getname(); system.err.println(filename); scan = new scanner(file); } grab (ioexception e) { system.err.println("io exception"); return; } } else { return; } words = new orderedlinkedlist<string>(); while(scan.hasnext()) { string token = scan.next().tolowercase(); token = token.replace(",","").replace(".",""); words.add(token); } scan.close(); textarea.append(" "+filename+" has wordcount: "+words.size()+ "\n-------------------------\n\n"); for(string word : words) { textarea.append(word+"\n"); // instead of displaying each word need read when word changes , list count of word... } } public static void main(string[] arg) { jframe frame = new jframe("oct 29"); frame.getcontentpane().add(new oct29()); frame.pack(); frame.setvisible(true); } }

there 2 ways can go this

1) reading them in maintain counter each word , when output can output counter

2) outputting maintain counter , when word changes output counter , word , set counter zero.

you can start in loop using string.equals. if true, increment. if false whatever need (assign value somewhere or output it) , set counter 0.

java list foreach count word

No comments:

Post a Comment