0

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.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
Quallssm
  • 13
  • 1
  • 6
  • The problem you're *really* trying to solve is "output data when two maps contain the same thing". For SO to help, you want to write the smallest possible program that does this -- so ditch the GUI and just show us a simple example with 2 hardcoded maps where you write your output to the console. You can add in the GUI later when the core program is working. – Greg Kopff Jun 03 '12 at 03:33
  • In fact, this is the same advice Andrew gave you when you posted your [other question](http://stackoverflow.com/questions/10826308/getting-keywords-from-two-files). Please take heed of it. – Greg Kopff Jun 03 '12 at 03:35

1 Answers1

4

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().

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • To clarify @Greg's answer a bit, the data structure you are using is a `Map`. `TreeMap` is one implementation (`HashMap` is another) but the implementation is a detail that doesn't affect the question. The answer would be the same if you were using a `HashMap`. – Jim Garrison Jun 03 '12 at 03:53
  • Wow Thanks, sorry for all of my mistakes. I have one more question, why is it when I call the maps later they no longer have information in them? – Quallssm Jun 03 '12 at 04:51
  • @Quallssm: Ah - see my edit. Since we're going to mutate the intersection set, we should use a set that is independent of the set returned by the map. – Greg Kopff Jun 03 '12 at 08:01
  • @GregKopff: I am very close now, The only problem now is that the values arent being pulled correctly. For example the two files I cam using the first file has the word apple in it 2 times, the second file has the word apple once. When I output it says "apples 1 1" when it should say "apples 2 1" Edit: It appears all of the values are being set to the highest, like if the word apple appears 3 and 2 times, all of the words values are set to 3 and 2. – Quallssm Jun 03 '12 at 08:34
  • Nevermind got it working fully now, I can't thank you enough for all of your help. And thanks for assisting me despite my poor posting etiquette. – Quallssm Jun 03 '12 at 08:51
  • @Quallssm: Glad to hear that you've solved it. Happy to help - and don't take the 'criticism' negatively ... we're also trying to help you learn how to post in a way that will enable others to help you most effectively. Please read the linked advice, and apply it next time you post a question. Also, note that an [SSCCE](http://sscce.org/) is even a useful debugging aid - if you distil your problem down to the most elementary program that exhibits it, it's *easier* for you to debug, not just for us. – Greg Kopff Jun 03 '12 at 09:51