Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.HeadlessException;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.LinkedList;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class Dictionary {
- private String fileName;
- private Collection<String> words;
- private long elapsedTime;
- public Dictionary(String fileName) { this.fileName = fileName; }
- public void loadDictionary(Collection<String> words) throws FileNotFoundException{
- this.words = words;
- Scanner input = new Scanner(new File(fileName));
- long startTime = System.currentTimeMillis();
- while(input.hasNext()){
- String word = input.nextLine();
- words.add(word);
- }
- input.close();
- elapsedTime = System.currentTimeMillis() - startTime;
- System.out.printf("%s build time in %s collection: %.2f seconds \n",
- fileName, words.getClass().getName(), (double) elapsedTime / 1000.0);
- }
- public void display(){
- System.out.printf("Contents of %s, organized using %s: \n",
- fileName, words.getClass().getName());
- int wordCount = 0;
- for(String word : words){
- System.out.print(word + " ");
- if(++wordCount % 20 == 0){ // new line every 20 words
- System.out.println();
- }
- }
- System.out.printf("\nCollection: %s Elapse Time:%.2f Word Count: %d\n",
- words.getClass().getName(), (double)elapsedTime / 1000.0, wordCount);
- JOptionPane.showMessageDialog(null, "Make note of the results, then click OK to continue...");
- }
- public static void main(String[] args){
- try{
- String fileName = JOptionPane.showInputDialog("Dictionary file name: ");
- Dictionary test = new Dictionary(fileName);
- test.loadDictionary(new LinkedList<String>());
- test.display();
- test.loadDictionary(new ArrayList<String>());
- test.display();
- }
- catch(HeadlessException e){ // can be generated by JOptionPane.showInputDialog()
- e.printStackTrace();
- }
- catch(FileNotFoundException e){
- System.out.println("File not found");
- }
- }
- }
Add Comment
Please, Sign In to add comment