Sorry, me again.

Hopefully the following will be enough code for you to help me:

Map<File,Integer> fileMap = new HashMap<File,Integer>();

private void displayResults()

{

// display the top 5 entries in the map in a text area

//if (searchString.equals(fileMap.get(searchString)))

resultText.setText(""); // init text area to empty string

for (int i = 0; i < 5; i++)

{// loop to 5

String word = findHighestScore();// find most common word

System.out.println("word = " + word);

resultText.append(String.format("%s (%d)\n", word, fileMap.get(file))); // add info to textarea

fileMap.remove(word);// remove this file from map

}

}

private String findHighestScore()

{

// find the word with highest count in the map

// iterate through map, checking each word to see if it has

String highestScoredFile = null; // initialise highest word

int countHighestWord = 0;// and highest word count

for (File file: fileMap.keySet())// iterate through map

if (fileMap.get(file) > countHighestWord)

{

highestScoredFile = file.getName();

countHighestWord = fileMap.get(file);

}

return highestScoredFile;

}

Now, this line of code is not removing the entry from the map, so it is being returned 5 times:

fileMap.remove(word);// remove this file from map

The following line of code returns null as the value for "fileMap.get(file)":

resultText.append(String.format("%s (%d)\n", word, fileMap.get(file)));

Message was edited by:

nessymonster

[1631 byte] By [nessymonstera] at [2007-10-3 10:34:57]
# 1

fileMap is a map from files to integers.

> Now, this line of code is not removing the entry from the map, so it is being

> returned 5 times:

> fileMap.remove(word); // remove this file from map

But word is a string, not a file. You are still allowed to try to remove it (which

may be surprising), but there is no way that the string word will be among

the files that make up the map's keys. So nothing will happen, as you observe.

> The following line of code returns null as the value for "fileMap.get(file)":

> resultText.append(String.format("%s (%d)\n", word, fileMap.get(file)));

get() will return null if the argument (in this case file) is not among the map's

keys. Note: in your posted code you don't show file being declared, or

used when adding an item to fileMap.

-

Please use the code tags described here: http://forum.java.sun.com/help.jspa?sec=formatting

Basically the idea is that you start your code with [code] and end it with [/code].

That way if you post something like

[code]if(tagsUsed) {

System.out.println("Output looks nice");

}[/code]

It will appear like if(tagsUsed) {

System.out.println("Output looks nice");

}

pbrockway2a at 2007-7-15 5:58:19 > top of Java-index,Java Essentials,Java Programming...
# 2

Are you going through a loop 5 times to pick out the File with the highest

associated integer and using this to build up some display text?

In this case consider having findHighestScore() return a File not a string. Ie

make highestScoredFile a File not a String, and say "highestScoredFile=file;"

not "highestScoredFile=file.getName();".

pbrockway2a at 2007-7-15 5:58:19 > top of Java-index,Java Essentials,Java Programming...
# 3
Nice one, thanks for that, got it.
nessymonstera at 2007-7-15 5:58:19 > top of Java-index,Java Essentials,Java Programming...
# 4
You're welcome.
pbrockway2a at 2007-7-15 5:58:19 > top of Java-index,Java Essentials,Java Programming...