I'm trying to write some code that will tell me if the first letter of one string equals the first letter of another. I can't figure out how to compare a string to the return of charAt(). Help?
public class CharAtTest
{
public static void main(String [] args)
{
String name = "joe";
String initial = "j";
if(initial.equals(name.charAt(0)))
{
System.out.println("Sucess");
}else{
System.out.println("Fail");
}
}
}
You could use:
if (initial.charAt(0) == name.charAt(0))
or better
if (name.startsWith(initial)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With