2

I have a text file with this information:

dog
sheep
cat
horse
cat
tiger
cat
cat
tiger

In another Q&A, I asked people how to count all the animal occurences, which I did with this method:

void countAnimals(){
    Map m1 = new HashMap();

    try (BufferedReader br = new BufferedReader(new FileReader("Animals.txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            String[] words = line.split(" ");//those are your words
            for (int i = 0; i < words.length; i++) {
                if (m1.get(words[i]) == null) {
                    m1.put(words[i], 1);
                } else {
                    int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
                    newValue++;
                    m1.put(words[i], newValue);
                }
            }
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Map<String, String> sorted = new TreeMap<String, String>(m1);
    for (Object key : sorted.keySet()) {
        System.out.println(key + "\t Votes: " + m1.get(key));
    }
}

This is what the console prints out:

cat  Votes: 4
dog  Votes: 1
horse    Votes: 1
sheep    Votes: 1
tiger    Votes: 2

Can someone help me on how to put the information, now printed to the console, into a two dimensional array, which I am going to put into a JTable?

Update: Here's what I added to my countAnimals method:

Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
    System.out.println(key + "\t Votes: " + m1.get(key));
}
AnimalSummaryModel SOCLOSE = new AnimalSummaryModel(sorted);
SOCLOSE.run();

And the AnimalSummaryModel class you have provided looks like this:

class AnimalSummaryModel extends AbstractTableModel {

    private final String[] keys;
    private Map<String, String> data;

    public AnimalSummaryModel(Map<String, String> data) {
        this.data = data;
        keys = data.keySet().toArray(new String[data.size()]);
    }

    public int getColumnCount() {
        return 2;
    }

    public int getRowCount() {
        return data.size();
    }

    public Object getValueAt(int row, int col) {
         if (col == 0) {
             return keys[row];
         } else {
             return data.get(keys[row]);
         }
    }

    private void display() {
        JFrame f = new JFrame("EnvTableTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(f, this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public void run() {
        display();
    }

}

I'm still getting nowhere.

Community
  • 1
  • 1
Gediminas
  • 51
  • 6

1 Answers1

2

Pass your Map<String, String> as a parameter to a custom TableModel and use the model to construct your JTable. A complete example is shown here.

Map<String, String> sorted = new TreeMap<>();
data.put("cat", "Votes: 4");
data.put("tiger", "Votes: 2");
data.put("horse", "Votes: 1");
data.put("sheep", "Votes: 1");
data.put("dog", "Votes: 1");
JTable table = new JScrollPane(new JTable(new AnimalSummaryModel(sorted)));
…
private static class AnimalSummaryModel extends AbstractTableModel {

    private final Map<String, String> data;
    private final String[] keys;

    public AnimalSummaryModel(Map<String, String> data) {
        this.data = data;
        keys = data.keySet().toArray(new String[data.size()]);
    }
    …
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You got it working, nice! I'm trying to implement your code, but can't seem to get it working yet. – Gediminas May 01 '16 at 16:43
  • @Gediminas: For the `data` parameter, I passed in a `TreeMap`, which you named `sorted`. Sorry for the confusion. – trashgod May 01 '16 at 16:57
  • @Gediminas: Start from the example cited, and make the changes shown; if you have problems, edit your question to include a [mcve] that shows your current approach. – trashgod May 01 '16 at 17:07
  • Why not have `countAnimals()` return a `Map`? – trashgod May 01 '16 at 17:17
  • Okay, let me try that – Gediminas May 01 '16 at 17:19
  • so in my case, the countAnimals() method returns "sorted". Then in AnimalSummaryModel i create an Object of my class that has a countAnimals() method, lets call it object. And then i type this: private Map data = object.countAnimals(); Am I correct? – Gediminas May 01 '16 at 17:28
  • Try`f.add(f, new JTable(new AnimalSummaryModel(sorted)))` in `main()`; post a [mcve] for additional guidance. – trashgod May 01 '16 at 20:26