Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:25:31+00:00 2026-06-04T21:25:31+00:00

I am trying to compare two treemaps and compare the keywords, then for the

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:25:31+00:00Added an answer on June 4, 2026 at 9:25 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem. I am trying to compare two datetime values in
I am trying to compare two decimal values in Java script. I have two
I am trying to compare two tools execution time, which I have installed in
I am trying to compare two dates. I have this code which I thought
I am trying to compare two tables to find rows in each table that
I have been trying to compare two arrays. Using array_intersect presents no problems. When
I am trying to compare two strings and remove any characters that appear in
I'm trying to compare two Xml files using C# code. I want to ignore
I'm trying to compare two arrays both 2d, I need it only match when
I'm trying to compare two values in the same table, and check if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.