PLEASE HELP I CANT MAKE THIS CODE WORK FOR ME,... JAVA import javax.swing.*; public class Dictionary {
Question:
PLEASE HELP I CANT MAKE THIS CODE WORK FOR ME,... JAVA
import javax.swing.*;
public class Dictionary { public static void main(String[] args){ WordList newDictionary = new WordList("school"); newDictionary.head.word.addDefinition("An Educational Facility."); viewMenu(newDictionary); } public static void viewMenu(WordList newDictionary){ String allDeletedWords = ""; for(String words : newDictionary.deletedWords){ //System.out.println(words); allDeletedWords += words + " "; } JTextArea deletedWordsList = new JTextArea(allDeletedWords); JScrollPane deletedScroll = new JScrollPane(deletedWordsList);
String[] options = {"Dictionary","Deleted","Cancel"}; int response1 = JOptionPane.showOptionDialog(null, "Welcome to the Dictionary App " + "Dictionary: shows dictionary options" + " Deleted: Shows a list of all deprecated words from the Dictionary" , "Dictionary Program", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null, options,options[0]);
if(response1 == 0) { while (true) { viewDictionary(newDictionary); } } else if(response1 == 1){ if(newDictionary.deletedWords.size() == 0){ int resultant = JOptionPane.showOptionDialog(null, "No Deleted Words", "Error", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); if(resultant == JOptionPane.OK_OPTION){viewMenu(newDictionary); } else{ viewMenu(newDictionary); } } for(String delWords : newDictionary.deletedWords){ int resultD = JOptionPane.showOptionDialog(null, deletedScroll,"Deleted Words", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); if(resultD == JOptionPane.OK_OPTION){ viewMenu(newDictionary); } else{ System.exit(0); } //System.out.println(delWords); } } else if (response1 == JOptionPane.CLOSED_OPTION || response1 == JOptionPane.CANCEL_OPTION){ System.exit(0); } }
public static void viewDictionary(WordList newDictionary){ String[] options2 = {"Add Word", "Delete Word", "View Dictionary", "Go Back"}; int response2 = JOptionPane.showOptionDialog(null, "Choose an Option", "Edit/View Dictionary", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null, options2,options2[0]); switch(response2){ case 0: try { String addWord = JOptionPane.showInputDialog(null, "Type the word you would like to add:"); addWord.toLowerCase(); if (!addWord.equalsIgnoreCase("") && addWord != null) { newDictionary.insertWord(addWord); String addDef = JOptionPane.showInputDialog(null, "Type the definition you would like to add to " + addWord); addDef.toLowerCase(); while(addDef.equalsIgnoreCase("") || addDef == null) { addDef = JOptionPane.showInputDialog(null, "Type the definition you would like to add to " + addWord); } newDictionary.insertDefinition(addWord, addDef); } else{ throw new RuntimeException("Cannot be empty input"); } } catch(RuntimeException e){ JOptionPane.showMessageDialog(null, "Empty Input Option Found", "Blasphemous Error", JOptionPane.ERROR_MESSAGE); } break; case 1: try { String delWord = JOptionPane.showInputDialog(null, "Type the word you would like to delete");
while (delWord.equalsIgnoreCase("") || delWord == null) { delWord = JOptionPane.showInputDialog(null, "Type the word you would like to delete"); }
newDictionary.deleteWord(delWord); } catch(NullPointerException e){ viewDictionary(newDictionary); } break; case 2: WordMeaningNode traverse = newDictionary.head; String allDictionaryWords = "";
while(traverse != null){ String multiDef = ""; for(String defs : traverse.word.Definitions){ multiDef +="\t-" + defs + " "; } allDictionaryWords += traverse.word.Word + " " + multiDef; traverse = traverse.next; } JTextArea dictionaryWords = new JTextArea(allDictionaryWords); JScrollPane dictionaryScroll = new JScrollPane(dictionaryWords);
int resultD = JOptionPane.showOptionDialog(null, dictionaryScroll,"Dictionary", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); if(resultD == JOptionPane.OK_OPTION){ viewDictionary(newDictionary); } else{ viewDictionary(newDictionary); }
break; case 3: viewMenu(newDictionary); break; case JOptionPane.CLOSED_OPTION: System.exit(0); break; } } }
WordList.java:
import javax.swing.*; import java.util.ArrayList;
public class WordList {
WordMeaningNode head; ArrayList
public void insertWord(String word){ WordMeaningNode wordNode = new WordMeaningNode(word); WordMeaningNode pointer = head; if(head == null){ head = wordNode; } else if(word.equalsIgnoreCase(pointer.word.Word) ){
} else if(word.compareTo(head.word.Word) < 0){ wordNode.next = head; head = wordNode; } else{ boolean resume = false; while(pointer.next != null && word.compareTo(pointer.next.word.Word) >= 0 ){ pointer = pointer.next; if(word.equalsIgnoreCase(pointer.word.Word)){ resume = true; } } if(!resume) { wordNode.next = pointer.next; pointer.next = wordNode; } }
}
public void insertDefinition(String word, String def){ WordMeaningNode pointer = head; while(!word.equalsIgnoreCase(pointer.word.Word) && pointer.next != null){ pointer = pointer.next; } pointer.word.addDefinition(def); }
public void deleteWord(String word){ WordMeaningNode pointer = head; if(pointer.word.Word.equalsIgnoreCase(word)){ head = head.next; deletedWords.add(word); System.out.println(deletedWords); } else{ try { while (pointer.next != null && !pointer.next.word.Word.equalsIgnoreCase(word)) { pointer = pointer.next; } if (pointer.next == null) { JOptionPane.showMessageDialog(null, "Word not found cannot delete", "Error", JOptionPane.ERROR_MESSAGE); } deletedWords.add(word); System.out.println(deletedWords.toString()); pointer.next = pointer.next.next; } catch(Exception e){ System.out.println(e); } } } }
WordMeaningNode.java:
public class WordMeaningNode { WordMeaningNode next; WordMeaning word;
public WordMeaningNode(){ next = null; word = new WordMeaning(""); }
public WordMeaningNode(String word){ this.word = new WordMeaning(word); } }
WordMeaning.java:
import java.util.ArrayList;
public class WordMeaning{
String Word; ArrayList
public WordMeaning(String title){ Word = title; }
public void addDefinition(String def){ Definitions.add(def); }
}
public class WordMeaningNode
WordMeaningNode next; WordMeaning word; public WordMeaningNode()
{
next = null;
word = new WordMeaning ("");
}
public WordMeaningNode (String word) { this.word = new WordMeaning (word);
}
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang