Hallo Leute,
ich habe ein Problem mit einer HashMap.
Ich habe ein Programm, welches eine kleine Wortstatistik erstellt.
Die Ergebnisse will ich in eine HashMap übergeben, um sie dann grafsich darstellen zu können...aber ich bekomm es nicht hin, weil ich nicht weiß, an welcher Stelle ich die HashMap definieren muss.
Folgender Code:
Zeile 59: dort wollte ich die HashMap füllen lassen, aber das geht so nicht weil er die HashMap dort nicht kennt...ich weiß auch nicht weiter....
Wäre echt klasse, wenn ihr mir helfen könntet....
VIELEN VIELEN DANK...MfG Marcus
ich habe ein Problem mit einer HashMap.
Ich habe ein Programm, welches eine kleine Wortstatistik erstellt.
Die Ergebnisse will ich in eine HashMap übergeben, um sie dann grafsich darstellen zu können...aber ich bekomm es nicht hin, weil ich nicht weiß, an welcher Stelle ich die HashMap definieren muss.
Folgender Code:
Code:
import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
class WordsStat
{
static boolean isWhiteSpace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static boolean isPunctuation(char c)
{
return c == '.' || c == '!' || c == '?' || c == ':' || c == ',' ||
c == '"' || c == '(' || c == ')' || c == ';' || c == '-';
}
static boolean ignore(char c)
{
return isWhiteSpace(c) || isPunctuation(c);
}
static String[] readWords(FileInputStream stream,int c,int index) throws IOException
{
while(c != -1 && ignore((char) c))
c = stream.read();
if(c != -1)
{
String word = "";
while(c != -1 && !ignore((char) c))
{
word += (char) c;
c= stream.read();
}
String[] words = readWords(stream,c,index + 1);
words[index] = word.toLowerCase();
return words;
}
else
return new String[index];}
static void count(String[] s)
{
int i = 0;
while(i < s.length)
{
String word = s[i++];
int num = 1;
while(i < s.length &&
word.equals(s[i++]))
++num;
hm.put(new String(word), new Integer(num));
System.out.println(num + "\t" + word);
}
}
static void swap(String[] s,int i, int j)
{
String t = s[i]; s[i] = s[j]; s[j] = t;
}
static void sort(String[] s)
{
for(int i = 0; i < s.length-1; ++i)
for(int j = i+1; j < s.length; ++j)
if(s[j].compareTo(s[i]) < 0)
swap(s,i,j);
}
static void main(String[] args) throws
FileNotFoundException,IOException
{
FileInputStream stream =
new FileInputStream(args[0]);
String[] words =
readWords(stream,stream.read(),0);
sort(words);
count(words);
}
}
public class MapDisplay {
public static void main(String[] args) {
HashMap hm = new HashMap();
MapDisplayTableModel tm = new MapDisplayTableModel(hm);
JTable table = new JTable(tm);
JScrollPane sp = new JScrollPane(table);
final JFrame frame = new JFrame();
frame.setTitle("Map Display");
frame.getContentPane().add(sp);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(true);
}
});
}
}
class MapDisplayTableModel extends AbstractTableModel{
List list;
String[] COLUMN_NAMES = {
"KEY",
"VALUE"
};
public MapDisplayTableModel(Map hm){
// this converts a Map into a List.
this.list = Collections.list(Collections.enumeration(hm.entrySet()));
}
public int getRowCount() {
return list.size();
}
public int getColumnCount() {
return COLUMN_NAMES.length;
}
public String getColumnName(int col){
return COLUMN_NAMES[col];
}
public Object getValueAt(int row, int col) {
Map.Entry entry = (Map.Entry)list.get(row);
switch(col){
case 0:
return entry.getKey().toString();
case 1:
return entry.getValue().toString();
}
return null;
}
}
Zeile 59: dort wollte ich die HashMap füllen lassen, aber das geht so nicht weil er die HashMap dort nicht kennt...ich weiß auch nicht weiter....
Wäre echt klasse, wenn ihr mir helfen könntet....
VIELEN VIELEN DANK...MfG Marcus