title: Parsing a string to a Java method
body: <p>I'm completely new to Java, so I'm sorry if the answer to this seems obvious...</p>
  
  <p>I've written a method in my class as follows:</p>
  
  <pre><code>private static final String[] rCode = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
  
    private int getCharValue(String code)
    {
      // loop through the codes to find a
      // matching code, if found exit loop (via return).
      for (int position = 0; position &lt; rCode.length; position++)
      {
        if (rCode[position] == code)
          return rVal[position];
      } // for
  
      // otherwise return 0
      return 0;
    } // getCharValue
  </code></pre>
  
  <p>In another method of the same class I am looping through the characters of a string as follows:</p>
  
  <pre><code>String number = "MMMCDXXXIV";
  
  for (int pos = 0; pos &lt; number.length(); pos++)
  { 
    System.out.println(number.substring(pos, pos + 1) + " " + getCharValue(number.substring(pos, pos + 1)));
  } // for
  </code></pre>
  
  <p>My problem is that while my getCharValue() method works fine when I hardcode a value in, e.g. <code>getCharValue("X")</code> it does not work when I call it like <code>getCharValue(number.substring(pos, pos + 1))</code> (even though I know <code>number.substring(pos, pos + 1)</code> is returning single characters which should be returning values.</p>
  
  <p>Can anyone see my obvious mistake?</p>
  
  <p>Many thanks...</p>