Daniel Camarena
Daniel Camarena

Reputation: 23

Can someone look at my code and tell me whats wrong?

So my requirement is to display a message showing yours and your friend's initials in lower case (ie. "mf and js are friends").

Here's my code

String myFullName = "Daniel Camarena";
String friendsFullName = "John Smith";

System.out.println(   myFullName.toLowerCase().charAt(0) 
                    + myFullName.toLowerCase().charAt(7)
                    + " and " 
                    + friendsFullName.toLowerCase().charAt(0)
                    + friendsFullName.toLowerCase().charAt(5)
                    + " are friends." );  

The output I get is
199 and js are friends.

Upvotes: 1

Views: 135

Answers (6)

Anuj Shah
Anuj Shah

Reputation: 1

It is adding ASCII value of d and c in output to avoid that do as following.

String myFullName = "Daniel Camarena";
        String friendsFullName = "John Smith";

        System.out.println(   myFullName.toLowerCase().charAt(0) 
                    +""+ myFullName.toLowerCase().charAt(7)
                    + " and " 
                    + friendsFullName.toLowerCase().charAt(0)
                    + friendsFullName.toLowerCase().charAt(5)
                    + " are friends." );  

Upvotes: 0

Karthik R
Karthik R

Reputation: 5786

With this one you can have any name of friends. Instead of correcting the index which differs for each name.

        String myFullName = "Daniel Camarena";
        String friendsFullName = "John Smith";

        String[] myNameSplit = myFullName.split(" ");
        String myFirstInitial = String.valueOf(myNameSplit[0].charAt(0));
        String myLastInitial = String.valueOf(myNameSplit[1].charAt(0));

        String[] myFriendNameSplit = friendsFullName.split(" ");
        String myFriendFirstInitial = String.valueOf(myFriendNameSplit[0].charAt(0));
        String myFriendLastInitial = String.valueOf(myFriendNameSplit[1].charAt(0));

        System.out.println(myFirstInitial+myLastInitial + " and " + myFriendFirstInitial+myFriendLastInitial+ " are friends");

Upvotes: 0

BhushanK
BhushanK

Reputation: 1243

You can use following code :

String myFullName = "Daniel Camarena";
    String friendsFullName = "John Smith";

    String[] arrMyFullName = myFullName.toLowerCase().split(" ");
    String[] arrFriendsFullName = friendsFullName.toLowerCase().split(" ");

    String message = "";
    for(String s : arrMyFullName)
        message += s.charAt(0);
    message += " and ";

    for(String s : arrFriendsFullName)
        message += s.charAt(0);
    message += " are friends.";

    System.out.println( message );

Above code also work if name is more than 2 words.

Upvotes: 1

Amila
Amila

Reputation: 5213

Try:

    System.out.println(  "" +  myFullName.toLowerCase().charAt(0)
            + myFullName.toLowerCase().charAt(7)
            + " and "
            + friendsFullName.toLowerCase().charAt(0)
            + friendsFullName.toLowerCase().charAt(5)
            + " are friends." );

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

myFullName.toLowerCase().charAt(0) + myFullName.toLowerCase().charAt(7) 

are working on ascii integer value and hence 199

The reason strings addition works for the second name is because that is part of the string formed due to this:

                + " and " 

Quick fix, add an empty string at start

System.out.println("" +   myFullName.toLowerCase().charAt(0) 
                    + myFullName.toLowerCase().charAt(7)
                    + " and " 
                    + friendsFullName.toLowerCase().charAt(0)
                    + friendsFullName.toLowerCase().charAt(5)
                    + " are friends." );  

Upvotes: 3

Panther
Panther

Reputation: 3339

   System.out.println(  "" + myFullName.toLowerCase().charAt(0)                       + myFullName.toLowerCase().charAt(7)
                + " and " 
                + friendsFullName.toLowerCase().charAt(0)
                + friendsFullName.toLowerCase().charAt(5)
                + " are friends." );  

Append the blank string to convert it to String and then it will start doing concanetation . As '+' is overloaded operator it is doing addition till it encounters String.

Upvotes: 1

Related Questions