Clarification 1 The user should be able to select a group of

Clarification

1) The user should be able to select a group of files to spell-check.

2) \"Multiple dictionaries\" implies the user should be able to use different dictionaries to spell-check the input files(one after the other).

At a time, the input file can interact with only one dictionary.

With respect to requirement 6, \"The program must handle multiple input files and dictionary files\", should the user be able to select a group of files to spell-check, or should it prompt for a new file after it completes one. Additionally, does \'multiple dictionaries\' mean that each file should be spell-checked with one or more dictionaries at the same time, or that each file should be spell-checked with its own choice of dictionary

Solution

we have Mistake class, Dictionary Class, SpellChecker class, and DictionaryGUI containing the main method.
NOTE:

* The Dictionary text file must contain one word per line
* Please edit the Mistake description (You can remove it though or use it for debugging purpose)

Dictionary Class:

public class Dictionary {
    class Node{
        Node left, right;
        String word;
        Node(String word){
            this.word = word;
        }
    }
    Node first = null;
    boolean hasWord(String searchWord) {
        Node temp = first;
        while (temp!=null){
            if(temp.word.equalsIgnoreCase(searchWord))
                return true;
            else if (temp.word.compareTo(searchWord)<0)
                temp = temp.left;
            else
                temp = temp.right;
        }
        return false;
    }
  
    boolean addWord(String newWord){
        if (first==null) {
            first = new Node(newWord);
            return true;
        }
        Node temp = first;
        boolean insertLeft = false, insertRight = false;
        while (true){
            if(temp.word.equalsIgnoreCase(newWord))
                return false;
            else if (temp.word.compareTo(newWord)<0) {
                if(temp.left==null){
                    insertLeft = true;
                    break;
                }
                temp = temp.left;
            }
            else
                if(temp.right==null){
                    insertRight = true;
                    break;
                }
                temp = temp.right;
        }
        if(insertLeft) {
            temp.left = new Node(newWord);
            return true;
        }
        else if(insertRight) {
            temp.right = new Node(newWord);
            return true;
        }
        return false;
    }
}


Mistakes class:


class Mistakes {
    String word;
    String reason;
    Mistakes next;
}

Spell Checker class:


public class SpellChecker {
    Dictionary dict;
  
    SpellChecker(File DictFile){
        loadDictionary(DictFile);
    }

    private void loadDictionary(File DictFile) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(DictFile));
            String word;
            dict = new Dictionary();
            while ((word=br.readLine().trim()) != null)
                dict.addWord(word);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SpellChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SpellChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
    Mistakes performSpellCheck(File textFile){
        Mistakes mistakes = null;
        Mistakes last = null;
        try {
            BufferedReader br = new BufferedReader(new FileReader(textFile));
            String line;
            while ((line =br.readLine().trim()) != null){
                int length = line.length();
                String word=null;
                for (int i=0; i<length; i++){
                    if (line.charAt(i) == \' \' || line.charAt(i) == \'.\' || line.charAt(i) == \',\' || line.charAt(i) == \';\' || line.charAt(i) == \':\'){
                        if (!dict.hasWord(word)) {
                            Mistakes temp = new Mistakes();
                            temp.word = word;
                            temp.reason = \"Word not found\";
                            if (mistakes==null) //mistake list is empty
                                mistakes = last = temp;
                            else{
                                Mistakes temp1 = mistakes;
                                while (temp1!=null){ //Find if similar word is already present in the mistake list
                                    if (temp.word.equalsIgnoreCase(temp1.word))
                                        break; //if present break the while loop
                                    temp1 = temp1.next;
                                }
                                if (temp1 == null)// if temp1 is null, we know that while loop executerd till the end and the new word is not duplicate
                                    last = last.next = temp;
                            }
                        }
                        word = \"\";
                    }
                    else
                        word = word + line.charAt(i);
                }
            }
          
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SpellChecker.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SpellChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mistakes;
    }
}

Dictionary GUI class:

public class DictionaryGUI extends javax.swing.JFrame {
    private File dictFile;
    private File textFile;
    private SpellChecker spellchecker;
    private Mistakes mistakes;

    /**
     * Creates new form DictionaryGUI
     */
    public DictionaryGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings(\"unchecked\")
    // <editor-fold defaultstate=\"collapsed\" desc=\"Generated Code\">                        
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        jButton2.setText(\"jButton2\");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText(\"Dictionary\");

        jLabel2.setText(\"File\");

        jTextField1.setText(\"jTextField1\");

        jTextField2.setText(\"jTextField2\");

        jButton1.setText(\"Browse\");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText(\"Browse\");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(\"Load\");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText(\"Load\");
        jButton5.setEnabled(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel3.setText(\"Mistakes:\");

        jLabel4.setText(\"No Mistake found\");

        jLabel5.setText(\"Load text to find mistake\");

        jButton6.setText(\"Ignore\");
        jButton6.setEnabled(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText(\"Add\");
        jButton7.setEnabled(false);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(jTextField2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton4))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton3)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5))))
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(34, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addGap(18, 18, 18)
                .addComponent(jButton7)
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3)
                        .addComponent(jButton5)))
                .addGap(28, 28, 28)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        JFileChooser chooser = new JFileChooser();
        //chooser.setFileFilter(txtFiles); // you can implement this
        chooser.setDialogTitle(\"Choose Dictionary file\");
        chooser.showOpenDialog(this);
        dictFile = chooser.getSelectedFile();
    }                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        JFileChooser chooser = new JFileChooser();
        //chooser.setFileFilter(txtFiles); // you can implement this
        chooser.setDialogTitle(\"Choose text file\");
        chooser.showOpenDialog(this);
        textFile = chooser.getSelectedFile();
    }                                      

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        spellchecker = new SpellChecker(dictFile);
        jButton5.setEnabled(true);
    }                                      

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        mistakes = spellchecker.performSpellCheck(textFile);
        getNextMistake();
    }                                      

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        spellchecker.dict.addWord(jLabel4.getText());
        getNextMistake();
    }                                      

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        getNextMistake();
    }                                      
  
    private void getNextMistake() {
        if (mistakes == null){
            jLabel4.setText(\"No more words found\");
            jLabel5.setText(\"\");
            jButton6.setEnabled(false);
            jButton7.setEnabled(false);
            return;
        }
        jLabel4.setText(mistakes.word);
            jLabel5.setText(mistakes.reason);
            jButton6.setEnabled(true);
            jButton7.setEnabled(true);
            mistakes = mistakes.next;
      
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
       
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if (\"Nimbus\".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DictionaryGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DictionaryGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DictionaryGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DictionaryGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DictionaryGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                 

}

Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \
Clarification 1) The user should be able to select a group of files to spell-check. 2) \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site