Skip to content

Tag: joke

Jedi Name

lightsaber

A simple Java class that calculates your Jedi name based on your first, last name, mother’s maiden name and the city you grew up.

/*
 Jedi first name: 1st 3 letters of last name + 1st 2 letters of first name
 Jedi last name: 1st 2 letters of mother's maiden name + 1st 3 letters of city you were raised in
 */

public class JediName {
   private static String capitalize(String s){
      return s.substring(0,1).toUpperCase() + s.substring(1,s.length()).toLowerCase();
   }

   private static String sub(String s, int begin, int end){
      return s.substring(begin, (s.length()>=end)?end:s.length());
   }

   public static String generate(String firstName, String lastName, String city, String mother ){
      String jediFirstName = capitalize(sub(lastName, 0, 3) + sub(firstName, 0,2));
      String jediLastName = capitalize(sub(mother,0,2)+ sub(city,0,3));
      return jediFirstName + " " + jediLastName;
   }
}

I used inline conditional expression ()?: to avoid raise a IndexOutOfBoundsException when the string is to small to cut the proper size we want.

And a example of how to use it:

public class JediNameTester {
	public static void main(String args[]){
		String myJediName = JediName.generate("Pedro", "Madeira", "Fortaleza", "Maria");
		System.out.println(myJediName);
	}
}

My jedi name is Siljo Iruba. 🙂