posted 20 years ago
If you cast a number to a character, you don't get a character (string?) that represents that number; you get the character whose Unicode value is that number. (char) 0 is not '0', but u0000, the ASCII "NUL" character. The numeral '0' has the Unicode value 48 decimal. You could get your program to work by writing
if (s.charAt(i) == j + 48) { ...
Now, a word of advice: the Java API is full of useful stuff. For this sort of very basic program, the java.lang package is worth looking at. In particular, if you're working with Strings and characters, read the whole API for String and Character classes. Character has methods for determining whether a char is whitespace (there are many other whitespace characters than just ' ') and whether a character is a digit (and it's more efficient than looping over the ASCII digits. Note also, again, that Unicode includes other digits than just the Arabic ones.)