gpt4 book ai didi

java - 如何覆盖TreeMap中的值?

转载 作者:行者123 更新时间:2023-12-02 05:50:54 24 4
gpt4 key购买 nike

因此,我尝试通过另一个文件扫描多个文件,以查看某个单词出现的次数,“其他文件”被获取并放置在树形图中,每次扫描文件中的下一个单词等于映射中的 key 应该将其值增加 1。无论出于何种原因,它都无法正确替换值。我几乎肯定错误就在这个区域

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
}
}

这就是全部内容

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);
}

}

它被分成2个src文件(所有system.out.prints用于测试)

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);
}

}

最佳答案

删除此行中的分号:

if (currentKey.equals(nextWord));

它充当if的主体,并且它下面的大括号中的 block 总是被执行。将其更改为

if (currentKey.equals(nextWord))

关于java - 如何覆盖TreeMap中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com