Hallo zusammen!!!!
Ich sitze momentan an einem kleinen programm um den inhalt von drei HashSets zu vergleichen. Das programm besteht aus zwei klassen.
Die klasse Hash erzeugt 3 HashSets und berechnet die schnittmenge von allen 3 Hashsets. Die klasse HashGui befasst 3 TextAreas, welche mit werten gefüllt werden sowie einen ActionButton.
Wenn der Actionbutton angeklickt wird, werden die werte aus den 3 TextAreas in die 3 Hashsets eingelesen und die Schnittmenge berechnet. In einem vierten Textfeld soll dann die schnittmenge ausgegeben werden.
An irgendeiner Stelle liegt jedoch ein fehler und ich kann ihn nicht finden. Etwas läuft mit der berechnung der schnittmengen falsch oder mit dem einlesen der daten in die HashSets. Oder natürlich beides.
Der meiste code den ich selbst geschrieben habe sitzt im ActionLister der gui klasse und der
Hashklasse.
Kann mir jemand weiterhelfen?
Liebe Grüße
Tilli
Und hier die klasse zum erzeugen der HashSets und der Schnittmengenberechnung.
Ich sitze momentan an einem kleinen programm um den inhalt von drei HashSets zu vergleichen. Das programm besteht aus zwei klassen.
Die klasse Hash erzeugt 3 HashSets und berechnet die schnittmenge von allen 3 Hashsets. Die klasse HashGui befasst 3 TextAreas, welche mit werten gefüllt werden sowie einen ActionButton.
Wenn der Actionbutton angeklickt wird, werden die werte aus den 3 TextAreas in die 3 Hashsets eingelesen und die Schnittmenge berechnet. In einem vierten Textfeld soll dann die schnittmenge ausgegeben werden.
An irgendeiner Stelle liegt jedoch ein fehler und ich kann ihn nicht finden. Etwas läuft mit der berechnung der schnittmengen falsch oder mit dem einlesen der daten in die HashSets. Oder natürlich beides.
Der meiste code den ich selbst geschrieben habe sitzt im ActionLister der gui klasse und der
Hashklasse.
Kann mir jemand weiterhelfen?
Liebe Grüße
Tilli
Java:
/*
* HashGuiView.java
*/
package hashgui;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
/**
* The application's main frame.
*/
public class HashGuiView extends FrameView {
public HashGuiView(SingleFrameApplication app) {
super(app);
initComponents();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String) (evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer) (evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = HashGuiApp.getApplication().getMainFrame();
aboutBox = new HashGuiAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
HashGuiApp.getApplication().show(aboutBox);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
jScrollBar1 = new javax.swing.JScrollBar();
jScrollBar2 = new javax.swing.JScrollBar();
jScrollBar3 = new javax.swing.JScrollBar();
jScrollPane4 = new javax.swing.JScrollPane();
jTextArea4 = new javax.swing.JTextArea();
jScrollBar4 = new javax.swing.JScrollBar();
jButton1 = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
jScrollPane5 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jPopupMenu1 = new javax.swing.JPopupMenu();
jPopupMenu2 = new javax.swing.JPopupMenu();
jPopupMenu3 = new javax.swing.JPopupMenu();
mainPanel.setName("mainPanel"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setName("jTextArea1"); // NOI18N
jScrollPane1.setViewportView(jTextArea1);
jScrollPane2.setName("jScrollPane2"); // NOI18N
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setName("jTextArea2"); // NOI18N
jScrollPane2.setViewportView(jTextArea2);
jScrollPane3.setName("jScrollPane3"); // NOI18N
jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jTextArea3.setName("jTextArea3"); // NOI18N
jScrollPane3.setViewportView(jTextArea3);
jScrollBar1.setName("jScrollBar1"); // NOI18N
jScrollBar2.setName("jScrollBar2"); // NOI18N
jScrollBar3.setName("jScrollBar3"); // NOI18N
jScrollPane4.setName("jScrollPane4"); // NOI18N
jTextArea4.setColumns(20);
jTextArea4.setRows(5);
jTextArea4.setName("jTextArea4"); // NOI18N
jScrollPane4.setViewportView(jTextArea4);
jScrollBar4.setName("jScrollBar4"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hashgui.HashGuiApp.class).getContext().getResourceMap(HashGuiView.class);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(57, 57, 57)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(76, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
.addGap(14, 14, 14)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollBar3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollBar2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollBar4, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE))))
.addGap(130, 130, 130))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hashgui.HashGuiApp.class).getContext().getActionMap(HashGuiView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 1247, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1077, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
jScrollPane5.setName("jScrollPane5"); // NOI18N
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setName("jList1"); // NOI18N
jScrollPane5.setViewportView(jList1);
jMenuBar1.setName("jMenuBar1"); // NOI18N
jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
jMenu1.setName("jMenu1"); // NOI18N
jMenuBar1.add(jMenu1);
jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
jMenu2.setName("jMenu2"); // NOI18N
jMenuBar1.add(jMenu2);
jPopupMenu1.setName("jPopupMenu1"); // NOI18N
jPopupMenu2.setName("jPopupMenu2"); // NOI18N
jPopupMenu3.setName("jPopupMenu3"); // NOI18N
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code her
if (evt.getSource() == jButton1) {
Hash.createHash();
//Get text From TextAreas
getText(jTextArea1, Hash.Hash1);
getText(jTextArea2, Hash.Hash2);
getText(jTextArea3, Hash.Hash2);
//Write to textField 4
hashToTextfield();
}
}
//Get Textlines form JTextARea
private void getText(JTextArea area, HashSet m) {
String text = area.getText();
int totalLines = area.getLineCount();
for (int i = 0; i < totalLines; i++) {
try {
int start = area.getLineStartOffset(i);
int end = area.getLineEndOffset(i);
String line = text.substring(start, end);
if(line.isEmpty() == false){
Hash.fillHash(line, m);
}
} catch (BadLocationException ex) {
Logger.getLogger(HashGuiView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//Write Content of HashFields to TextArea
private void hashToTextfield() {
HashSet h1 = new HashSet();
h1 = Hash.Intersect(Hash.Hash1, Hash.Hash2, Hash.Hash3);
//System.out.print(val);
Iterator itr = Hash.Hash1.iterator();
while (itr.hasNext()) {
jTextArea4.append(itr.next().toString());
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JPopupMenu jPopupMenu2;
private javax.swing.JPopupMenu jPopupMenu3;
private javax.swing.JScrollBar jScrollBar1;
private javax.swing.JScrollBar jScrollBar2;
private javax.swing.JScrollBar jScrollBar3;
private javax.swing.JScrollBar jScrollBar4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
private javax.swing.JTextArea jTextArea4;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}
Und hier die klasse zum erzeugen der HashSets und der Schnittmengenberechnung.
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hashgui;
import java.util.*;
/**
*
* @author jazz
*/
public class Hash {
public static HashSet Hash1, Hash2, Hash3;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
//Create 3 HashFields
public static void createHash(){
Hash1 = new HashSet();
Hash2= new HashSet();
Hash3 = new HashSet();
}
//Fill Hashset with String values.
public static void fillHash(String value, HashSet m){
m.add(value);
}
//Calculate intersection between Three Hashsets.
public static HashSet Intersect(HashSet m1, HashSet m2, HashSet m3) {
HashSet Tempintersection = new HashSet(m1);
Tempintersection.retainAll(m2);
HashSet intersection = new HashSet(Tempintersection);
intersection.retainAll(m3);
return intersection;
}
}