samedi 28 mars 2015

Typecast to convert String to Char

So I have this palindrome program where the constructor will remove all the punctuations from string s and store it into s2. My teacher does not allow me to use the charAt method, only allowing the use of the substring method. When I try to typecast a substring to char and then int to see if the character is a letter, I get a compiler error saying it cannot cast string to char. Why?



public Palindrome(String s)
{
s1 = s;
for(int i = 0; i < s.length(); i++)
{
if((((int) Character.valueOf(s.substring(i, i + 1))) >= 65) && (((int) Character.valueOf(s.substring(i, i + 1))) <= 90))
{
if((((int) Character.valueOf(s.substring(i, i + 1))) >= 97) && (((int) Character.valueOf(s.substring(i, i + 1))) <= 122))
{
s2 += s.substring(i, i + 1);
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire