Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Delivering the MVP Applying a Discount Code

Unsure how to proceed with checking whether a String only contains letters and the $ symbol.

Hi I am unsure of how to check whether or not a String only contains letters and the $ symbol. In the video we had only gone for the class it had only gone over checking whether a char is a letter with Character.isLetter method however i am not sure Strings have the same method or what to do for this challenge task 2.

Any help is appreciated.

1 Answer

Richard Lambert
PLUS
Richard Lambert
Courses Plus Student 7,180 Points

Hello mate,

The String class has the toCharArray() method which converts a string to an array of characters. From there, you can use an enhanced for loop to iterate over each character element within the array for validation.

String name = "Christopher";
char[] lettersInName = name.toCharArray(); 
for (char eachLetter : lettersInName) { 
    //validation goes here
}

Hope this helps

Fahad Mutair
Fahad Mutair
10,359 Points

Great explanation and brief, 1 for you.