Question: Hello, need some java help on adding 1 thing to my current code. I need this to have a graphical interface that deplays my output
Hello, need some java help on adding 1 thing to my current code. I need this to have a graphical interface that deplays my output after click a button.
Here is my code thanks for the help.
import java.io.File;
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner;
public class WordCount { static String[] invalidWords={"the","and","it","a","on","in"};//list of words not to be considered public static void main(String[] args) { List wordsInFirst=new ArrayList(); List wordsInSecond=new ArrayList(); File firstFile = new File("D:\\workspace\\chegg\\src\\wordcount\\How the Old Man Made People.txt"); File secondFile = new File("D:\\workspace\\chegg\\src\\wordcount\\Mistakes of Old Man.txt"); wordsInFirst= inputFromFile(firstFile); wordsInSecond=inputFromFile(secondFile); List uniqueWords1 = setFrequencies(wordsInFirst); List uniqueWords2 = setFrequencies(wordsInSecond); System.out.println("Top 10 words in first File: "); System.out.println("Word Frequency Percentage"); System.out.println("---- --------- ----------"); List topTen1=new ArrayList(); for (int i=0;i<=10;i++) { double size=uniqueWords1.size(); double frequency=uniqueWords1.get(i).getFrequency(); double percent=(frequency/size)*100; System.out.println(uniqueWords1.get(i).toString()+" "+Math.round(percent)+"%"); topTen1.add(uniqueWords1.get(i).getValue()); } System.out.println("Top 10 words from Second File: "); System.out.println("Word Frequency Percentage"); System.out.println("---- --------- ----------"); List topTen2=new ArrayList(); for (int i=0;i<=10;i++) { double size=uniqueWords2.size(); double frequency=uniqueWords2.get(i).getFrequency(); double percent=(frequency/size)*100; System.out.println(uniqueWords2.get(i).toString()+" "+Math.round(percent)+"%"); topTen2.add(uniqueWords2.get(i).getValue()); } listOfCommonWords(topTen1,topTen2); } /* * Method to read words from the file and set to the list */ public static List inputFromFile(File myFile) { List words=new ArrayList(); Scanner in; try { // read the details from the given file in = new Scanner(myFile); while(in.hasNext()) { String nextWord=in.next(); //read the next word through scanner nextWord=nextWord.replace(",", ""); //replace commas"," appended at the end of the file nextWord=nextWord.replace(".", ""); //replace ."," appended at the end of the file nextWord=nextWord.replace("\"", ""); //replace double quotes"," appended at the end of the file if(!Arrays.asList(invalidWords).contains(nextWord)){ //check if the word is not the one from the list of words to be removed words.add(nextWord); } } in.close(); } catch (FileNotFoundException e) { System.out.println("Input file not found : " + myFile); System.exit(1); } // Read from input file and get the list of words return words; } public static List setFrequencies(List words){ List newList=new ArrayList();//List of words with their frequencies List addedList=new ArrayList(); for (String word : words) { Word w=new Word(); if(!addedList.contains(word)){//Condition to make sure the added word is not added. w.setValue(word); w.setFrequency(Collections.frequency(words,word)); newList.add(w); addedList.add(word); } } Collections.sort(newList,new Word()); return newList; } /* * returns common Words from top 10 words of both files */ public static List listOfCommonWords(List wordsList1, List wordsList2){ List commonWords=new ArrayList(); for (String word : wordsList1) { if(wordsList2.contains(word)){//Condition to make sure the added word is not added. commonWords.add(word); } } System.out.println("Common Words among top 10 from both files: "+commonWords); return commonWords; }
}
Word.java this is used to set frequency and implement compareTo method to sort using frequency
import java.util.Comparator;
public class Word implements Comparator{ private String value; private int frequency; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public int getFrequency() { return frequency; } public void setFrequency(int frequency) { this.frequency = frequency; } //Override to String to print the required value and frequency public String toString(){ return " "+value+" "+frequency; }
//To sort based on frequencies
@Override public int compare(Word w1, Word w2) { return w2.frequency-w1.frequency; } }