Last active
August 29, 2015 14:26
-
-
Save Maslor/4f3b8bf660b9c1b1f7df to your computer and use it in GitHub Desktop.
Receives an array of quotes and the name of the hero. Said hero will have one of its letters replaced by a number, which indicates which quote he says.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
public class BatmanQuotes{ | |
public static int indexOf(Pattern pattern, String s) { | |
Matcher matcher = pattern.matcher(s); | |
return matcher.find() ? matcher.start() : -1; | |
} | |
public static String getQuote(String[] quotes, String hero){ | |
int n; | |
String returnString = ""; | |
int index = indexOf(Pattern.compile("[(0-9)]"), hero); | |
if(index == hero.length()-1) n = Integer.parseInt(hero.substring(index)); | |
else n = Integer.parseInt(hero.substring(index,index+1)); | |
String[] split = hero.split("[(0-9)]"); | |
if (indexOf(Pattern.compile("(Rob)"),split[0]) != -1) return returnString+="Robin: " + quotes[n]; | |
else if (indexOf(Pattern.compile("(Bat)"),split[0]) != -1) return returnString+="Batman: " + quotes[n]; | |
return returnString+="Joker: " + quotes[n]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment