i am having a problem with multipling a simple int value (1) with the values from a char-array:
public void doStuff(String values)
{
char[] numbers = values.toCharArray();
int result = 1;
for (int i = 0; i < numbers.length; i++)
{
System.out.println(numbers[i] + " " + result);
result *= numbers[i];
}
// more stuff here
}
in my case the input string-parameter is “73167”. in the line “result *= numbers[i]” result should be 7 after the first iteration, but debug info in eclipse says that result = 55.
any hints??
Your problem is that
"73167"isn’t a number. It’s a string made of the characters"7","3","1","6","7".charin Java are Unicode code points, and'7'is 55 in ASCII/UTF-8. You want to doDoc for: Character.getNumericValue(char)