Question: Use a TreeMap to Sort the Words This project should work the same as project 3, opening a chosen file and displaying the unsorted Words

Use a TreeMap to Sort the Words

This project should work the same as project 3, opening a chosen file and displaying the unsorted Words on the left and the sorted Words on the right. For this project do not use the arraylist to sort the Words, but rather use a TreeMap (adapt the code from the PowerPoint on Maps).

Create Edit Menu in your GUI

Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a Word. The program should then use a JOptionPane message dialog to tell if the word is in the TreeMap or not. You will need to write second menu handler called EditMenuHandler.

Below is project 3 code

IllegalWordException

public class IllegalWordException extends IllegalArgumentException {

public IllegalWordException(String msg) {

super(msg);

}

public IllegalWordException() {

super("Invalid Word!");

}

}

Project3

public class Project3 {

public static void main(String[] args){

WordGUI gui=new WordGUI("File Chooser App");

}

}

Word

import java.util.Comparator;

import java.util.regex.Pattern;

public class Word implements Comparator{

private final String value;

public Word(String value){

if(!Pattern.matches("^[A-za-z]+$", value.trim()))

{

this.value=value;

throw new IllegalWordException(value);

}

else{

this.value=value;

}

}

Word() {

value=null;

}

public String getValue(){

return this.value;

}

/**

*

* @param o1

* @param o2

* @return

*/

@Override

public int compare(Word o1, Word o2) {

Word w1 = (Word)o1;

Word w2 =(Word)o2;

return w1.value.compareTo(w2.value);

}

}

WordGUI

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.StringTokenizer;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.BorderFactory;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JTextArea;

import javax.swing.filechooser.FileNameExtensionFilter;

public class WordGUI extends JFrame implements ActionListener

{

private final ArrayList invalidWords;

private final ArrayList UnsortedValidWords;

private final ArrayList SortedvalidWords;

public WordGUI(String title)

{

super(title);

invalidWords=new ArrayList<>();

UnsortedValidWords=new ArrayList<>();

SortedvalidWords=new ArrayList<>();

setBounds(40,40,800,800);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu file = new JMenu("File");

menuBar.add(file);

JMenuItem open = new JMenuItem("Open");

open.addActionListener(this);

JMenuItem quit = new JMenuItem("Quit");

quit.addActionListener(this);

file.add(open);

file.add(quit);

this.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent ae) {

String choice = ae.getActionCommand();

if (choice.equals("Quit")) {

System.exit(0);

}

else if (choice.equals("Open")) {

Word newWord=new Word();

JFileChooser Choose=new JFileChooser();

FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");

Choose.setFileFilter(filter);

int i=Choose.showOpenDialog(this);

if(i==JFileChooser.APPROVE_OPTION){

File f=Choose.getSelectedFile();

String filepath=f.getAbsolutePath();

try {

BufferedReader br = new BufferedReader(new FileReader(filepath));

String line=null;

while ((line = br.readLine()) != null){

line=line.toLowerCase();

StringTokenizer tokens = new StringTokenizer(line, " ");

while (tokens.hasMoreTokens()){

String word = tokens.nextToken();

try{

newWord=new Word(word);

UnsortedValidWords.add(newWord);

SortedvalidWords.add(newWord);

}

catch(IllegalArgumentException E){

invalidWords.add(word);

}

}

}

Collections.sort(SortedvalidWords,new Word());

} catch (FileNotFoundException ex) {

// file not found

Logger.getLogger(WordGUI.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(WordGUI.class.getName()).log(Level.SEVERE, null, ex);

}

display();

}

}

}

public void display()

{

JTextArea b1 = new JTextArea("Sorted Words: "+getWords(SortedvalidWords));

b1.setBorder(BorderFactory.createLineBorder(Color.BLACK));

b1.setLineWrap(true);

JTextArea b2 = new JTextArea("UnSorted Words: "+getWords(UnsortedValidWords));

b2.setBorder(BorderFactory.createLineBorder(Color.BLACK));

b2.setLineWrap(true);

JTextArea b3 = new JTextArea("Invalid Words: "+getWordsFromString(invalidWords));

b3.setBorder(BorderFactory.createLineBorder(Color.BLACK));

b3.setLineWrap(true);

this.add(b1);

this.add(b2);

this.add(b3);

// setting dimention of 1 row and 3 columns

this.setLayout(new GridLayout(1, 3));

this.setVisible(true);

}

private String getWords(ArrayList Array) {

StringBuilder buffer = new StringBuilder();

for (int i=0;i

String word=Array.get(i).getValue();

if (word != null) {

if (buffer.capacity() == 0) {

buffer.append(word);

} else {

buffer.append(" ").append(word);

}

}

}

return buffer.toString();

}

private String getWordsFromString(ArrayList Array){

StringBuilder buffer = new StringBuilder();

for (int i=0;i

String word=Array.get(i);

if (word != null) {

if (buffer.capacity() == 0) {

buffer.append(word);

} else {

buffer.append(" ").append(word);

}

}

}

return buffer.toString();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!