i basically need a java program that will translate english sentences into pirate talk. The real focus here is on using a Map (Hashmap). This is what I have so far.
First of all, I used a translation table and put it into the hash map. After that I used a for loop, so the program can loop through the input and find words that need to be translated.
Let me show you an example: "the professor wants to know if there is a restaurant nearby."
should be translated to:
"th' cap'n wants t' know if there be a galley broadside. Arrr."
But my output looks like this:
th cap'n wants t' know if there be a galley nearby.
If it is an "end-of-sentence word" the translation must be printed out, followed by a "Arr". ("hey." --> "avast. Arr.")
My code does not do this. I tried some codes but none of them worked correctly.
I am using Junit Test. So another problem I am facing is, if I translate any word, lets just say "hello" it is giving me the translation "ahoy " with a space in it.
How do I avoid this?
I am new to Java, I would appreciate any help I can get.
package pirate;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PirateTranslator
{
public static void main(String[] args)
{
Map<String, String> hashmap = new HashMap<>();
hashmap.put("hello", "ahoy");
hashmap.put("hi", "yo-ho-ho");
hashmap.put("hey", "avast");
hashmap.put("my", "me");
hashmap.put("friend", "me bucko");
hashmap.put("sir", "matey");
hashmap.put("madam", "proud beauty");
hashmap.put("stranger", "scurvy dog");
hashmap.put("officer", "foul blaggard");
hashmap.put("where", "whar");
hashmap.put("is", "be");
hashmap.put("are", "be");
hashmap.put("the", "th");
hashmap.put("you", "ye");
hashmap.put("your", "yer");
hashmap.put("you're", "ye be");
hashmap.put("we're", "we be");
hashmap.put("old", "barnacle-covered");
hashmap.put("attractive", "comely");
hashmap.put("happy", "grog-filled");
hashmap.put("nearby", "broadside");
hashmap.put("restroom", "head");
hashmap.put("restaurant", "galley");
hashmap.put("hotel", "fleabag inn");
hashmap.put("bank", "buried treasure");
hashmap.put("yes", "aye");
hashmap.put("yes!", "aye aye!");
hashmap.put("addled", "mad");
hashmap.put("after", "aft");
hashmap.put("money", "booty");
hashmap.put("professor", "cap'n");
hashmap.put("food", "grub");
hashmap.put("of", "o'");
hashmap.put("quickly", "smartly");
hashmap.put("to", "t'");
hashmap.put("and", "an'");
hashmap.put("it's", "it be");
hashmap.put("right", "starboard");
hashmap.put("left", "port");
Scanner scan = new Scanner(System.in);
String token = scan.nextLine();
String[] result = token.split("\\s");
for (int i = 0; i < result.length; i++)
{
if (hashmap.containsKey(result[i]))
{
result[i] = hashmap.get(result[i]);
}
System.out.print(result[i] + " ");
}
}
}