2

If I have a

String myName = "First Last";

How can I, without an if or any other conditionals/loops, return just the initials of the name??

I've been looking forever on this!

Thanks

5
  • Is this homework or something? I can think of plenty of ways that don't appear to use conditionals, but none that don't actually use them. Commented Oct 12, 2011 at 2:01
  • 1
    Why without conditionals or loops? What have you tried so far? And Brendan Long is correct, no matter what, you'll end up getting BNE or equivalent code when it's running. Commented Oct 12, 2011 at 2:01
  • No, I'm just trying to get most efficient way to search a String. I thought maybe there is a method to find where each space in a string is or something.. Commented Oct 12, 2011 at 2:05
  • 1
    @Matt - The most efficient way is to use a loop and if statement ;) Commented Oct 12, 2011 at 2:07
  • 1
    Are you allowed to use goto? :) Commented Oct 12, 2011 at 2:17

4 Answers 4

6

Since you can't loop or use conditionals, you can't check anything, so this is your only choice:

/**
 * Gets the initials from a String. Note: Only works on the String "First Last"
 */
String getInitials(String s) {
    return "FL";
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1: Thinking outside The Box. You found the answer I couldn't think of.
5

Without some form of branching/looping/conditionals somewhere this is impossible (to handle in a generic fashion), but who says we need to do the branching? (Regular expressions are powerful beasts -- sometimes too powerful, and always beasts...)

String name = "First Last";
String initials = name.replaceAll("[^A-Z]", "");

Please note that your mileage will vary: consider Steve McQueen (or Президент Российской Федерации) as counter examples. Modification of the regular expression above is a viable solution (that is "left as an exercise" ;-), but pay heed to the warning about regular expressions.

Happy coding.

1 Comment

Love the pathological counterexamples. Like fire, regular expressions are only dangerous if they go out of control and get all over everything.
2
  1. Use charAt(int) method to get a character.
  2. Find location of last name using indexOf(String) method

The sample code is below.

String initial_name = myName.charAt(0)+"."+myName.charAt(myName.indexOf(' ')+1)+".";

1 Comment

Good one! I feel a little dumb for not thinking of that!
0

OK, how about this:

String myName = "First Last";
String[] names = myName.split("\s+");
return names[0] + names[1];

Note of course that this only works for exactly two names separated by some whitespace. Finding a different number of initials probably requires a regex or some conditionals :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.