Checking Code: Getting the Unicode value of each character from a string.
/* Something is wrong here not exactly sure i am a beginner. I cannot use ANY classes.
EXAMPLE: if argument value is "ABC" then the return value should be "198".
Help! Thanks in advance^:)^*/
public class Test {
public static void main(String[] args) {
addChars("ABC");
}
public static int addChars ( String s ) {
int sum = 0;
for (int i=0; i<s.length();i++) {
if(s.charAt(i) <= s.length()) {
sum += (int)s.charAt(i); // adds Unicode value to sum
}
else {
return 0;
}
}
return sum;
}
}
Re: Checking Code: Getting the Unicode value of each character from a string.
I'm not entirely sure what you mean, but have a look at what this line is doing, is it ever true?
if(s.charAt(i) <= s.length())
Re: Checking Code: Getting the Unicode value of each character from a string.
Well every character has value i have to produce that value. I would believe that it is true. My string is "ABC" so as i access the string with s.charAt(i) grabbing that first character 'A' it is less than or equal too the length of the String.