-3

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?

2 Answers2

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
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 :)

  • 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