How to Abbreviate a Two Word Name in Java

The challenge

Write a function to convert a name into initials. This challenge strictly takes two words with one space in between them.

The output should be two capital letters with a dot separating them.

It should look like this:

Sam Harris => S.H

Patrick Feeney => P.F

The solution in Java

This is a really easy one, basically what we will do is the following:

  1. Convert our name toUpperCase so that we can just pull out the characters we want.
  2. Convert the name String into 2 separate values, stored in a names array.
  3. Return the first character of each, using charAt(0) with a dot inbetween.
public class AbbreviateTwoWords { public static String abbrevName(String name) { name = name.toUpperCase(); String[] names = name.split(" "); return names[0].charAt(0)+"."+names[1].charAt(0); } }
Code language: Java (java)

Test cases to validate our solutionn

import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testFixed() { assertEquals("S.H", AbbreviateTwoWords.abbrevName("Sam Harris")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("Patrick Feenan")); assertEquals("E.C", AbbreviateTwoWords.abbrevName("Evan Cole")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("P Favuzzi")); assertEquals("D.M", AbbreviateTwoWords.abbrevName("David Mendieta")); assertEquals("Z.K", AbbreviateTwoWords.abbrevName("Zenon Kapusta")); assertEquals("G.C", AbbreviateTwoWords.abbrevName("george clooney")); assertEquals("M.M", AbbreviateTwoWords.abbrevName("marky mark")); assertEquals("E.D", AbbreviateTwoWords.abbrevName("eliza doolittle")); assertEquals("R.W", AbbreviateTwoWords.abbrevName("reese witherspoon")); } @Test public void testRandom() { for(int i = 0; i < 200; i++){ String testString = makeString(); assertEquals(randomTest(testString), AbbreviateTwoWords.abbrevName(testString)); } } private String makeString() { return makeWord(1, 20) + " " + makeWord(1, 20); } private String makeWord(int min, int max) { String word = ""; String[] possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""); int length = (int) (Math.random() * max) + min; for(int i=0; i < length; i++) { int index = (int) (Math.random() * possible.length); word += possible[index]; } return word; } private String randomTest(String name){ String[] nameSplit = name.toUpperCase().split(" "); return nameSplit[0].substring(0, 1) + '.' + nameSplit[1].substring(0, 1); } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments