We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Showing HashMap result in a String
Page Index Toggle Pages: 1
Showing HashMap result in a String (Read 3010 times)
Showing HashMap result in a String
Jun 16th, 2010, 8:51am
 
I had problems while trying to show the result of my HashMap in a String. When I Iterate through the HashMap, I get the name of the elements in it and an Integer containing the number of times it appears. My problem appears when I try to get my results in a String to show it to the user. If I fill the String with the current element, I get only the last one. If I type "myString = myString + currentElement + "\n";", the results are up to date every time but the keep going under each other because of the "\n". I want myString to be placed somewhere in the screen so that everytime I add the same element in my HashMap it keeps the count up to date.

Here's the code :
Code:
HashMap words = new HashMap();
String restant = "",name;

void setup() {
size(500,500);
}

void draw() {
background(0);
stroke(255);
line(width/2,0,width/2,height);
line(0,height/2,width,height/2);
try {
text(words.get("rightUp").toString(), width/2+10,10);
text(words.get("rightDown").toString(), width/2+10,height/2+10);
text(words.get("leftUp").toString(), 10,10);
text(words.get("leftDown").toString(), 10,height/2+10);
text(restant,5,height/2-40);
} catch (NullPointerException e) {}
}

void mouseReleased() {
if (mouseX > 0 && mouseX < width/2 && mouseY > 0 && mouseY < height/2) {
addWord("leftUp");
}
else if (mouseX > width/2 && mouseX < width && mouseY > 0 && mouseY < height/2) {
addWord("rightUp");
}
else if (mouseX > width/2 && mouseX < width && mouseY > height/2 && mouseY < height) {
addWord("rightDown");
}
else if (mouseX > 0 && mouseX < width/2 && mouseY > height/2 && mouseY < height) {
addWord("leftDown");
}
else if (mouseX == width/2) {
addWord("COQUITLAM");
}
else if (mouseY == height/2) {
addWord("MEDICINE HAT");
}
printWords();
}

void addWord(String value) {
Integer n = (Integer)words.get(value);
int count = (n!=null ? n.intValue()+1 : 1);
words.put(value,new Integer(count));
}

void printWords() {
Iterator iter = words.keySet().iterator();
while(iter.hasNext()){
name = iter.next().toString();
if (name.equals("rightUp") == false && name.equals("leftUp") == false && name.equals("rightDown") == false && name.equals("leftDown") == false) {
println(name + " " + words.get(name));
if (keyPressed) {
restant = restant + "\n" + name + " " + words.get(name);
}
}
}
}
Re: Showing HashMap result in a String
Reply #1 - Jun 16th, 2010, 12:08pm
 
I noticed that my question was a bit hard to understand.
To make it simple, I need to count unknown data.
For example, I read an array of words from a .txt file and if the focussed word appears more than one time, I need to add the number of times it appears next to the word in a String.  Huh
And this must be done for multiple words.
Re: Showing HashMap result in a String
Reply #2 - Jun 16th, 2010, 11:57pm
 
I'm not sure I understood your question, but here are some comments on your code - I hope this helps

In the draw method, instead of catching the NullPointerException and ignoring it, you should check if the value is null. Once the exception is thrown the following lines of code are not executed, which may lead to unexpected results.

Instead of
Code:
try {
 text(words.get("rightUp").toString(), width/2+10,10);
 text(words.get("rightDown").toString(), width/2+10,height/2+10);
 text(words.get("leftUp").toString(), 10,10);
 text(words.get("leftDown").toString(), 10,height/2+10);
 text(restant,5,height/2-40);
 } catch (NullPointerException e) {}


I would use a helper function to test if the value is null
Code:
  text(getNum("rightUp"), width/2+10,10);
 text(getNum("rightDown"), width/2+10,height/2+10);
 text(getNum("leftUp"), 10,10);
 text(getNum("leftDown"), 10,height/2+10);
 text(restant,5,height/2-40);

Code:
String getNum(String w) {
 Integer n = (Integer)words.get(w);
 if (n == null)
   return "0";
  return n.toString();
}


The second thing is that you should probably initialize the String you are using for the results at the beginning of the  printWords method to an empty String. It should look something like this:
Code:
void printWords() {
 restant = "";
 Iterator iter = words.keySet().iterator();
 while(iter.hasNext()){
   name = iter.next().toString();
   if (name.equals("rightUp") == false && name.equals("leftUp") == false && name.equals("rightDown") == false && name.equals("leftDown") == false) {
println(name + " " + words.get(name));
   }
   restant = restant + name + " " + words.get(name) + " ";
}


Hope this helps  Smiley
Re: Showing HashMap result in a String
Reply #3 - Jun 17th, 2010, 4:46am
 
Yes, I was perplex by your first message...
I was about to make the same remark than guyy, NPE catching is inappropriate here (and elsewhere in general), as if one word isn't found, all the next ones are skipped...

You can iterate on the HashMap using entrySet(): thus you can check the value and if above 1, get the key and use both.
Re: Showing HashMap result in a String
Reply #4 - Jun 17th, 2010, 6:09am
 
I never tought that it could be a solution to empty the string each time I add a value! XD   Thanks alot for that!

Also, do you know why there are functions imported from pure java and supported by Processing that aren't colored in orange like the other keywords?
Re: Showing HashMap result in a String
Reply #5 - Jun 17th, 2010, 6:26am
 
Eau-Lit wrote on Jun 17th, 2010, 6:09am:
Also, do you know why there are functions imported from pure java and supported by Processing that aren't colored in orange like the other keywords

Probably because they are too many! PDE only colors Processing-specific keywords (with a bit of mix up when a word is both a variable and a function (like mousePressed) and Java keywords that are used often enough (like ArrayList).
Re: Showing HashMap result in a String
Reply #6 - Jun 17th, 2010, 6:32am
 
Ok so there is a list of words that fry and REAS choosed to be the most common used that are colored. When I started learning Processing my teacher used those keywords that wern't colored and I was always mystified!

Thanks for your time PhiLho and again, thanks guyy for those precious tips!
Page Index Toggle Pages: 1