Question

I am trying to compare two treemaps and compare the keywords, then for the ones that both maps have in common I want to add the values together and then output this into a text area

So far I have two treeMaps that are succesfully working, but I can't figure out how to compare them and the rest.

Here is my code so far:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


public class AnalysisFrame extends JFrame
{
private static final int FRAME_WIDTH = 370;
private static final int FRAME_HEIGHT = 700;

JPanel displayPnl, filePnl, fileOnePnl, fileTwoPnl, textPnl, controlPnl, buttonPnl, infoPnl;
JTextArea resultTA;
JButton quitBtn, oneBtn, twoBtn, goBtn;
JScrollPane resultSP;
Border blackline, empty;
Label infoLbl;
File fileOne, fileTwo, finalFile;

public AnalysisFrame()
{
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    displayPnl = new JPanel();

    createFilePanel();
    displayPnl.add(filePnl);

    createTextPanel();
    displayPnl.add(textPnl);

    createControlPanel();
    displayPnl.add(controlPnl);

    add(filePnl, BorderLayout.NORTH);
    add(textPnl, BorderLayout.CENTER);
    add(controlPnl, BorderLayout.SOUTH);

    this.setTitle("Steven Qualls - CP3 Final");
}

private void createFilePanel() {
    filePnl = new JPanel();

    createFileOnePanel();
    filePnl.add(fileOnePnl);

    createFileTwoPanel();
    filePnl.add(fileTwoPnl);



}

            public void createFileOnePanel() {
                fileOnePnl = new JPanel();

                TitledBorder fileOneB;
                blackline = BorderFactory.createLineBorder(Color.black);
                fileOneB = BorderFactory.createTitledBorder(blackline, "File 1");
                fileOneB.setTitleJustification(TitledBorder.CENTER);
                fileOnePnl.setBorder(fileOneB);

                oneBtn = new JButton("Choose File");

                fileOnePnl.add(oneBtn);
                oneBtn.setPreferredSize(new Dimension (100,30));

                        class OneButtonListener implements ActionListener
                        {
                            @Override
                            public void actionPerformed(ActionEvent evt)
                            {
                                final JFileChooser oneFC = new JFileChooser();
                                oneFC.showOpenDialog(AnalysisFrame.this);
                                String newLine = null;
                                oneFC.getName(null);
                                int returnVal = 0;
                                File fileOne = oneFC.getSelectedFile();

    Scanner input = null;
            try {
                input = new Scanner(fileOne);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(AnalysisFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

    String inputText = input.nextLine();

    String[] words = inputText.split("[ \n\t\r,.;:!?(){}]");

    TreeMap<String, Integer> mapOne = new TreeMap<>();

    for(int i = 0; i < words.length; i++){
        String key = words[i].toLowerCase();

        if (words[i].length() > 1){
            if (mapOne.get(key) == null){
                mapOne.put(key, 1);

            }
            else {
                int value = mapOne.get(key).intValue();
                value++;
                mapOne.put(key, value);


            }
        }


    }
    /* Experimental Set Code

    Set<String> setOne = mapOne.keySet();
    for (String key : setOne)
    {
        Integer value = mapOne.get(key);

    }
    * 
    */



                            }

                        }
                        ActionListener oneListener = new OneButtonListener();
                        oneBtn.addActionListener(oneListener);

            }



            public void createFileTwoPanel() {
                fileTwoPnl = new JPanel();

                TitledBorder fileTwoB;
                blackline = BorderFactory.createLineBorder(Color.black);
                fileTwoB = BorderFactory.createTitledBorder(blackline, "File 2");
                fileTwoB.setTitleJustification(TitledBorder.CENTER);
                fileTwoPnl.setBorder(fileTwoB);

                twoBtn = new JButton("Choose File");

                fileTwoPnl.add(twoBtn);
                twoBtn.setPreferredSize(new Dimension (100,30));

                        class TwoButtonListener implements ActionListener
                        {
                            @Override
                            public void actionPerformed(ActionEvent evt)
                            {
                                final JFileChooser twoFC = new JFileChooser();
                                twoFC.showOpenDialog(AnalysisFrame.this);
                                String newLine = null;
                                twoFC.getName(null);
                                int returnVal = 0;
                                File fileTwo = twoFC.getSelectedFile();

                                        Scanner input = null;
            try {
                input = new Scanner(fileTwo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(AnalysisFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

    String inputText = input.nextLine();

    String[] words = inputText.split("[ \n\t\r,.;:!?(){}]");

    TreeMap<String, Integer> mapTwo = new TreeMap<>();

    for(int i = 0; i < words.length; i++){
        String key = words[i].toLowerCase();

        if (words[i].length() > 1){
            if (mapTwo.get(key) == null){
                mapTwo.put(key, 1);

            }
            else {
                int value = mapTwo.get(key).intValue();
                value++;
                mapTwo.put(key, value);


            }
        }


    }


                            }
                        }
                        ActionListener twoListener = new TwoButtonListener();
                        twoBtn.addActionListener(twoListener);
            }




private void createTextPanel() { 
    textPnl = new JPanel();

    resultTA = new JTextArea(100, 25);
    resultTA.setEditable(false);
    resultTA.setLineWrap(true);
    resultTA.setWrapStyleWord(true);

    resultSP = new JScrollPane(resultTA);

    textPnl.add(resultTA);
    textPnl.add(resultSP);
}

private void createControlPanel() {
    controlPnl = new JPanel();
    controlPnl.setLayout(new GridLayout(2,1));


    createInfoPanel();
    controlPnl.add(infoPnl);

    createButtonPanel();
    controlPnl.add(buttonPnl);


}

            private void createInfoPanel() {
                infoPnl = new JPanel();

                JLabel infoLbl = new JLabel("Execute will find and output words both files have in common.", JLabel.CENTER);
                infoPnl.add(infoLbl);
            }

            private void createButtonPanel() {
                buttonPnl = new JPanel();

                goBtn = new JButton("Execute");
                quitBtn = new JButton("Quit");

                buttonPnl.add(goBtn);
                buttonPnl.add(quitBtn);

                goBtn.setPreferredSize(new Dimension (100,30));
                quitBtn.setPreferredSize(new Dimension (100,30));





                class QuitButtonListener implements ActionListener
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {
                        System.exit(0);
                    }
                }

                        ActionListener quitListener = new QuitButtonListener();
                        quitBtn.addActionListener(quitListener);

                class GoButtonListener implements ActionListener
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {


                    }
                }
                        ActionListener goListener = new GoButtonListener();
                        goBtn.addActionListener(goListener);

            }


}

Here is some pseudo code that I thought up, I am not sure if this is possible or will work but it was the only way I could think up how to do it

Get a word from mapOne
Check if word exists in mapTwo //This part is going to be the hardest I'm pretty sure
If word is found, get the key and set as freq2 then remove from mapTwo //Not sure if this is even needed
If word is not found in mapTwo, remove word from mapOne
Check mapOne.isEmpty if true, end loop

I also seem to have a problem with my brackets or something, I am new to programming so it could be something similar, but when I make my treemaps, I can output them succesfully in the same block of code, but if I go to the bottom and try to output elsewhere it comes up as null or not found, like the map only exists withen that area, I know I should know why this happening but I dont, I am assuming something to do with public or private, or something.

Thanks for any help in advance, sorry it so large of a question, I am just very lost.

Was it helpful?

Solution

I am trying to compare two treemaps maps and compare the keywords keys, then for the ones that both maps have in common I want to add concatenate the values together and then output this into a text area.

final Set<String> intersection;
final Set<String> values2;

// Since we're going to mutate the first set, we create a new set that is 
// completely independent from the map.  We fill it with they keys of the 
// first map.

intersection = new HashSet<String>(map1.keySet());  // get the first map's keys
values2 = map2.keySet();                            // ... and the second map's keys

intersection.retainAll(values2);                    // find the intersection

for (String key : intersection)                     // for each value in both sets ...
{                                                   // ... output the details
  System.out.println("key: " + key);
  System.out.println("value 1: " + map1.get(key));
  System.out.println("value 2: " + map2.get(key));
}

See: Set.retainAll().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top