Hi guys. Bit of a problem from me again. This time its not homework cos its not wednesday Its an exercise i found on the net
But what I’m wondering is, how do i get a program to ignore a word(i.e String) if its not contained in an array.
Here’s what I’m working on:
public class PirateTranslater
{
static String TranslateToPirate(String userWord)
{
String translatedForBlackBeard = "";
String space = " ";
String [] pirateVocabulary = {"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 & Skuppers", "buried treasure"};
String [] userText = {"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"};
for(int i = 0; i < userText.length; i++)
{
if(userWord.equals(userText[i]))
{
translatedForBlackBeard = pirateVocabulary[i];
}
else
{
return userWord ;
}
}
return translatedForBlackBeard;
}
public static void main(String [] args)
{
System.out.println("Say somethin' to ol' BlackBeard!");
String word = Console.readToken();
while(word != null)
{
String convertedWord = TranslateToPirate(word);
// deal with the word
System.out.print(convertedWord + " ");
//read in the next word
word = Console.readToken();
}
}
}
Here’s the problem. If i remove the else statement in the method, it prints out all the words that can be translated(i.e those contained in userText) but doesn’t print the ones that aren’t contained in the method, i.e such as ‘there’ and ‘could’. But if i do include the else staement, it just returns the original string. So any thoughts from you guys??
By the way. I’m using IO redirection for it, and here’s the input in a .txt file:
hello there sir
could you tell me where is the nearest restaurant
Thats my input!
And again, I stress its NOT homework, I’m just trying to build my skills!
And I appreciate everything you guys do for me!
Thx
Hauk