Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

charAt() return not a string?

Tags:

java

char

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");
            }

        }
    }
like image 478
Jon Takagi Avatar asked May 16 '26 02:05

Jon Takagi


1 Answers

You could use:

if (initial.charAt(0) == name.charAt(0))

or better

if (name.startsWith(initial)) {
like image 186
Reimeus Avatar answered May 17 '26 15:05

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!