my dog learned polymorphism*
The moose likes Beginning Java and the fly likes why won't charAt work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why won Watch "why won New topic

why won't charAt work?

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I've written this code to create an array containing all the letters in a string. However, originally in my method, I used charAt, but I kept getting an error message. So, I tried substring and it worked fine. Can anyone tell me why charAt wouldn't work in this case? Thanks.

This is where I used charAt originally....
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Well if you were trying to do ca[ i ] = s.charAt( i ), you would get an error because you're trying to put a char type into a String array, they are incompatible. You would need to change ca to an array of characters, not an array of Strings.
By the way, in case you didn't know, there is a method called toCharArray() in class String, that does exactly what you are looking for.
subject: why won't charAt work?
Similar Threads
Prob with String arrays
Printing out the combinations of a given string
how to perform string operations on string object ?
[Easy] Shift String
Assigning values to variables in constructors