Question

So I'm trying to scan multiple files through one other file to see how many times a word appears, the "other file" is taken and placed in a tree map and each time the next word in the scanned file equals a key in the map that key is suppose to have its value increased by 1. For whatever reason it is not correctly replacing the values. I'm almost positive the error is in this area

while(fileArrayScan.hasNext())
{ 
                        try
                        {
                            String nextWord = fileArrayScan.next();
                            for(String key : tagsCount.keySet())
                            {
                                String currentKey = key;

                                if (currentKey.equals(nextWord));
                                {
                                    int value = tagsCount.get(key);
                                    tagsCount.put(key, value + 1);
                                }
                            }
                        }
                        catch(NoSuchElementException ex)
                        {
                            //if there is a random empty line it catches it
                        }
}

Here is the whole thing

import javax.swing.JFrame;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hurtks
 */
public class TagCloudGeneratorRunner {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        JFrame frame = new TagCloudGeneratorFrame();
        frame.setTitle("Kyle's Tag Cloud Generator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

It's split into 2 src files (all the system.out.prints where for testing)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hurtks
 */

public class TagCloudGeneratorFrame extends JFrame
{

public TagCloudGeneratorFrame()
{

//set the arraylist
final ArrayList<File> fileArray = new ArrayList();

//set the height and width of the frame
final int HEIGHT = 600;
final int WIDTH = 500;
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setLocation(screenWidth / 4, screenHeight / 4);

//set the panels that are going to be used
final JPanel mainPnl, chooserPnl, filePnl, controlPnl;

//set the buttons that are going to be used
final JButton chooseFileBtn, submitBtn, quitBtn;

//ser the file chooser
final JFileChooser chooser, tagChooser;
chooser = new JFileChooser();
tagChooser = new JFileChooser();


//set the textarea and scrollpane that are going to be used
final int AREA_ROWS = 10;
final int AREA_COLUMNS = 25;
//final int value = 0;

final JTextArea fileNameArea;
fileNameArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
fileNameArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(fileNameArea);
fileNameArea.setText("");

//create the panels and add them to the frame
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
chooserPnl = new JPanel();
filePnl = new JPanel();
controlPnl = new JPanel();
mainPnl.add(chooserPnl, BorderLayout.NORTH);
mainPnl.add(filePnl, BorderLayout.CENTER);
mainPnl.add(controlPnl, BorderLayout.SOUTH);

add(mainPnl);
setSize(HEIGHT, WIDTH);

//create the components
chooseFileBtn = new JButton("New File");
submitBtn = new JButton("Submit");
quitBtn = new JButton("Quit");


//add the components to the GUI
chooserPnl.add(chooseFileBtn);
filePnl.add(scrollPane);
controlPnl.add(submitBtn);
controlPnl.add(quitBtn);

//add the listener to the quitBtn
class QuitButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent evt)
    {
        System.exit(0);
    }
}
ActionListener quitListener = new QuitButtonListener();
quitBtn.addActionListener(quitListener);

//add the listener to the chooseFileBtn
class chooseFileButtonListener implements ActionListener
{
    //set the arraylist
    //ArrayList<File> fileArray = new ArrayList<>();
    public void actionPerformed(ActionEvent evt)
    {
        if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            File selectedFile = chooser.getSelectedFile();
            fileArray.add(selectedFile);
            fileNameArea.append("" + selectedFile + "\n");
            //return fileArray;

        }
    }
}
ActionListener chooseListener = new chooseFileButtonListener();
chooseFileBtn.addActionListener(chooseListener);

//add the listener to the submitBtn
class submitButtonListener implements ActionListener
{
    Map<String, Integer> tagsCount = new TreeMap();
    Scanner tagScanner;
    //JFileChooser tagChooser;
    //tagChooser = new JFileChooser();
    //PrintWriter output = new PrintWriter("htmlFile.txt");
    public void actionPerformed(ActionEvent evt)
    {

        try
        {
            JOptionPane.showMessageDialog(null, "Please Choose the file of Tags to scan for.");
            if(tagChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            {
            File tagFile = tagChooser.getSelectedFile(); 
            String fileDir = tagFile.getParent();
            File outputFile = new File(fileDir, "htmlFile.txt");
            PrintWriter output = new PrintWriter(outputFile);
            //File tagFile = tagChooser.getSelectedFile();
            tagScanner = new Scanner(tagFile); 
            while(tagScanner.hasNext())
            {
                tagsCount.put(tagScanner.next().toLowerCase(), 0);
            }
            for (int j = 0; j < fileArray.size(); j++)
            {
                File thisFile = fileArray.get(j);
                //Scanner for File Array
                Scanner fileArrayScan = new Scanner(thisFile);
                while(fileArrayScan.hasNext())
                { 
                    try
                    {
                        String nextWord = fileArrayScan.next();
                        System.out.println(nextWord + " next word in file");
                        for(String key : tagsCount.keySet())
                        {
                            String currentKey = key;
                            System.out.println(currentKey + " Current Key");

                            if (currentKey.equals(nextWord));
                            {
                                System.out.println(currentKey + " Current Key in if loop");
                                int value = tagsCount.get(key);
                                System.out.println(value + " value for key map");
                                tagsCount.put(key, value + 1);
                            }
                        }
                    }
                    catch(NoSuchElementException ex)
                    {
                        //if there is a random empty line it catches it
                    }
                }
                //System.out.println(thisFile);

            }
            output.println(tagsCount);
            System.out.println(tagsCount);
            JOptionPane.showMessageDialog(null, "The File was saved as 'htmlFile.html'");

            output.flush();
            output.close();         
        }
        }
        catch(FileNotFoundException ex)
        {
                    fileNameArea.append("The File could not be found, Try Again!");
        }
    }
}
ActionListener submitListener = new submitButtonListener();
submitBtn.addActionListener(submitListener);
}

}
Was it helpful?

Solution

Remove the semicolon from this line:

if (currentKey.equals(nextWord));

It acts the body of the if, and the block in braces below it is always executed. Change it to

if (currentKey.equals(nextWord))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top