java - String converting to Char and then to Int -


i'm writing palindrome function school take string argument , remove punctuation. purpose of class, have use substring() method.

my code takes substring of each character in string argument , converts char, , int. then, checks if integer within ascii numbers a-z or a-z , if is, substring appended blank string (s2).

my main issue when try compile program, error saying substring can't converted char. ideas on why?

  public palindrome(string s)     {         for(int = 0; < s.length(); i++)         { //                                      checks if character uppercase letter             if((int) character.valueof(s.substring(i, + 1)) >= 65)                 if((int) character.valueof(s.substring(i, + 1)) <= 90)                     s2 += s.substring(i, i+1); //                                      checks if character lowercase letter             if((int) character.valueof(s.substring(i, + 1)) >= 97)                 if((int) character.valueof(s.substring(i, + 1)) <= 122)                     s2 += s.substring(i, i+1);         }     } 

a string object is not character or char, if length of string one. casting trying fool compiler, not change actual underlying object.

if string#charat() forbidden, can use string#tochararray() return char[] (of length 1 in case). e.g.

char[] mychar = s.substring(i, + 1); // mychar[0] contains character 

then test if it's letter, can use

character.isletter(mychar[0]); 

beware when cast

(int)character.valueof(...) 

a character object returned valueof(), not char. work, because compiler unboxing of character char.

if want use ascii code instead of character#isletter(), can do:

char[] mychar = s.substring(i, + 1); int thecharasint = (int)mychar[0]; // proceed test range  

because native char value can read native int value.

int codea = (int)'a';  // codea == 65 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -