for example, if I want to say that c=4 and d=4, then c*d=16? I'm reading both c and d values from input. my sample test case is: c = 45 d = 45. I've got it store the c into a variable called charVal and 45 is stored into value. When I insert statement like, charVal=value, it gives me errors saying incompatible types. how would I do that?
Asked
Active
Viewed 821 times
-3
-
Why do you want to store it in a char? Why can't int or byte? Any specific reason? – Pradeep Simha Apr 14 '15 at 15:13
-
@DanielNugent it gives me a"_" an output. – Dexter Morgan Apr 14 '15 at 15:29
-
It's actually `-`, and that's expected for `45`. Take a look at the ASCII table: http://www.asciitable.com/ – Daniel Nugent Apr 14 '15 at 15:32
2 Answers
1
You want to use a map.
The java.util.Map interface represents a mapping between a key and a value.
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("c", 4);
map.put("d", 4);
System.out.println(map.get("c") + map.get("d"));
Returns
8

Eric S.
- 1,502
- 13
- 31
-
-
-
@DexterMorgan It's good to know though, you can't use a primitive (int), in a Generic. The generic must derive from java.lang.Object. Integer does, but int is a primitive and it doesnt. – Eric S. Apr 14 '15 at 15:31
-
it gives me an out put a dash, "-". I'm not sure where I'm going wrong. – Dexter Morgan Apr 14 '15 at 15:33
-
I just ran it, and it gives me 8. You must have something else going wrong. – Eric S. Apr 14 '15 at 15:34
-
0
You could also use 2D arrays where you use row[0] column[0] to store characters and row[0] column[1] to store integers or vice versa and for any character you go ahead take its respective column value and multiply it.
And finally convert your 2D String array to Int using following method:
int y = Integer.parseInt(str);
Hope that helps :)

Raghvendra Gupta
- 73
- 10
-
I'm already doing that converting the input string into the an integer. My question is how do I assign a value to a char? for example, if I have C as char, how do I say c=4, when I do c*c, i should get 16? – Dexter Morgan Apr 14 '15 at 15:35