All pastes #1705961 Raw Edit

Miscellany

public java v1 · immutable
#1705961 ·published 2009-12-07 20:01 UTC
rendered paste body
/* Testing:No matches, should return exactly what I put in (unless by chance an "Arrr. " is thrown in after the full stops:Enter the String (Ctrl+d to finish): Hey, testing this program, etc., etc.Output: Hey, testing this program, etc., etc.Example string given by Charlie:Enter the String (Ctrl+d to finish): Hello there Sir. Could you tell me where the nearest Restaurant is?Output: Ahoy there Matey. Arrr. Could ye be tellin' me whar th' nearest Galley be? Arrr.To test if it matches some phrases with more than one word each ("Excuse me" and "Pardon me"Enter the String (Ctrl+d to finish): Excuse me. Pardon me. Could you tell me where is the nearest restaurant?Output: Arrr. Avast. Arrr. Could ye be tellin' me whar be th' nearest galley?*/import java.util.Random;public class BlackTalk{	//List of words/phrases in English and the pirate equivalent	static String [] engWords = { "hello", "hi", "pardon me", "excuse me", "my", "friend", "sir", "madam", "miss", "stranger", "where", "is", "the",							"you", "tell", "know", "how far", "old", "attractive", "happy", "nearby", "restroom", "restaurant", "hotel", "pub", "bank"};	static String [] pirWords = { "ahoy", "yo-ho-ho", "avast", "arrr", "me", "me bucko", "matey", "proud beauty", "comely wench", "scurvy dog", "whar",							"be", "th'", "ye", "be tellin'", "be knowin'", "how many leagues", "barnacle-covered", "comely", "grog-filled",							"broadside", "head", "galley", "fleabag inn", "Skull & Scuppers", "buried treasure"};		//Checks if any English phrase is at the very start of string inputted and get the index of it in engWords, -1 if not found	static int getMatchIndex(String input)	{		for (int i=0; i<engWords.length; i++)		{			//Only check if the phrase isn't bigger than the input, since obviously can't be contained in it otherwise			if (!(engWords[i].length() > input.length()))			{				//Gets the right amount of letters and converts to lowercase				String temp = input.substring(0, engWords[i].length());				temp = temp.toLowerCase();				if (temp.compareTo(engWords[i]) == 0)				{					//It's a match, but make sure it doesn't end in the middle of a word ("there" would match "the" and "high" match "hi", but both should be ignored)					if (input.length() == engWords[i].length())					{						//Same length so obviously doesn't end halfway through a word						return i;					}					else if (!Character.isLetter(input.charAt(engWords[i].length())))					{						//The next character isn't a letter, so doesn't end halfway through a word						return i;					}				}			}		}		return -1;	}	//Just replaces the first letter of the phrase with a capital if needed	static String capitalise(String input)	{		String out = "";		//If first character is lowercase letter convert it (by subtracting 32), then output the rest as is.		if (Character.isLowerCase(input.charAt(0)))		{			out += (char)(input.charAt(0) - 32);			out += input.substring(1);			return out;		}		else		{			//Otherwise just return it as is.			return input;		}	}		public static void main(String [] args)	{		//Gets input string		System.out.print("Enter the String (Ctrl+d to finish): ");				//Hint says to just handle one word at a time, but some phrases "excuse me" for example take more than one word so can't really do it this way?		//So we build up a string.		String english = "";		String word = Console.readToken();		while (word != null)		{			english += word + " ";			word = Console.readToken();		}				String pirate = "";		//Keeps index of current word character.		int currWord = 0;		//Go through the phrase, skipping to next space or new line each time so we don't compare starting in middle of words (waste of time even if		//it doesn't mess things up).		do		{			//Gets the phrase we've matched (or -1)			int matchIndex = getMatchIndex(english.substring(currWord));			if (getMatchIndex(english.substring(currWord)) != -1)			{				//We got a real match, if the start of the match is a capital we capitalise the output as well, otherwise just use output as given.				String output = pirWords[matchIndex];				if (Character.isUpperCase(english.charAt(currWord)))				{					output = capitalise(output);				}				pirate += output;				//Move along to the last letter in matched string to skip words if needed.				currWord += engWords[matchIndex].length();			}					//Gets next word index			int nextSpace = english.indexOf(' ', currWord);			if (nextSpace == -1)			{				//If there's no more set it to the last character				nextSpace = english.length() - 1;			}			//Output whatever remains between the end of this word and start of next (punctuation and spaces) and set currWord to the start of next one.			String output = english.substring(currWord, nextSpace+1);			pirate += output;			//If the above output contains a full stop, question mark or exclamation mark then 1 in 3 chance to add "Arrr. "			if ((output.indexOf('.') != -1) || (output.indexOf('!') != -1) || (output.indexOf('?') != -1)) 			{				Random gen = new Random();				int a = gen.nextInt(3);				if (a == 1)				{					pirate += "Arrr. ";				}			}			currWord = nextSpace + 1;		} while(currWord < english.length()); //Ends when no more words 		//Finally, output translated message		System.out.println("Output: " + pirate);	}}