We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All!
I get unexpected token: > at line 3. Why?
class Mix {
private HashMap<String, color> hm = new HashMap<String, color>();
hm.put("910", color(232, 200, 95));
hm.put("930", color(227, 189, 96));
hm.put("960", color(212, 131, 92));
}
PS: is there an escape character to use greater/less than symbols in the question title?
Answers
color is Processing specific and when converting to java code it is replaced in the preprocessor by int, which is a primitive dataype, not an Object. You need an Object type, so in this case that would be Integer. The corrected line is thus:
HashMap <String, Integer> hm = new HashMap <String, Integer> ();
Then to answer your next question: "but now I get unexpected token: ( at line 5. Why?". You can't call methods there. You should format your class properly, by using variables, a constructor and methods. The corrected class is:
A Java's Collection, like Map for example, can't use primitive types!
In your case,
color
is a syntactic sugar for primitiveint
! And its corresponding wrap class is Integer:http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
final HashMap<String, Integer> hm = new HashMap();
There's another bug: in order to run statements outside methods, we gotta place them inside curly braces:
Careful that doing so fails in JS Mode, unless it's not a nested class! And Mix is a nested inner class btW! :-SS
Alternatively, you can use the Processing 2's new class: IntDict, which accepts primitive types as values:
http://processing.org/reference/IntDict.html
Although it still uses a HashMap<String, Integer> internally called
indices
: 8-Xhttps://github.com/processing/processing/blob/master/core/src/processing/data/IntDict.java#L26
Well, I use
<
for "less-than" myself! ;;)P.S.: An excellent explanation about data-types. Worth the reading: :D
docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Those keys "910", "930", "960" can be represented as numbers perfectly.
So why do you wanna them to be String btW? /:) Isn't an Integer key enough?! L-)
BtW, I've found this explanation of mine in some old thread of yours: ;;)
http://forum.processing.org/two/discussion/6717/passing-variable-name-to-method
int
orfloat
in them.All right! Thanks a lot, guys.
amnon:
You should format your class properly
Of course. I forgot you must initilize variables in the constructor.
GoToLoop:
Those keys "910", "930", "960" can be represented as numbers perfectly.
Yes, perfectly true, but these are names in a standard a color model, some of which contain letters. The fact that only names made of digits appear in my example is simple awkwardness on my part for the post.
GoToLoop:
I've found this explanation of mine in some old thread of yours
You bet. That's the thread that got the HashMap thing rolling! I didn't know what that type did before.
Wrap class... Is a wrap class the one another extends from?
EDIT: OK I read this and I've understood it. Is this what a wrapper class is also called?
"Must" is too strong of a word which rarely happens to be true in programming and anything else btW! :>
Generally, fields are initialized either "in situ" or further ahead inside a constructor or a delegated method for it.
However, when a field happens to refer to a data container, it's not such a bad idea to have an initialization block right next to it. Keeping both declarations & initializations close together makes code easier to study too! O:-)
A wrapper class is 1 that encapsulates some data in order to be used in some place which otherwise would be incompatible if used "in natura"!
More specifically I've meant Java's 8 primitive wrapper classes:
https://en.wikipedia.org/wiki/Primitive_wrapper_class