2

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] + " ");
        }
    }
}
user5909208
  • 95
  • 2
  • 12
  • Put in logic to 1) check for punctuation, 2) remove punctuation when doing word replacement, 3) replace punctuation (without the space) after replacement, 4) add terminal phrase if punctuation is a terminating mark. I'm betting you can do this. – Hovercraft Full Of Eels Mar 30 '16 at 23:35
  • 2
    What's a pirate's favorite programming language? You'd think it was R but really it's The C – Neil McGuigan Mar 31 '16 at 00:13
  • @HovercraftFullOfEels I am really struggling with "Arr" after the period. Any other tips? Meanwhile I solved the "space" thing. – user5909208 Mar 31 '16 at 00:39

3 Answers3

2

Just be adding this as th' very last line o' yer code,

if (token.endsWith("."))
    System.out.print(" Arrr.");

There are other issues, eg "left" as a direction should be translated, but not as the past tense of the verb "to leave", ie "I left it behind" shouldn't translated to "I port it behind", isn't that starboard?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Is this adequate? His/her other main problem is that her HashMap isn't recognizing terms that contain terminal punctuation (see my comment to her question). – Hovercraft Full Of Eels Mar 31 '16 at 00:13
  • "Arr" should be added only if there is a period in a sentence. For example "hello sir." --> "ahoy matey. Arr". Never thought about translating past tense:) I will keep that in mind. – user5909208 Mar 31 '16 at 00:20
  • @hover I answered the first atomic question encountered. The other part should be edited out and a new question asked. – Bohemian Mar 31 '16 at 01:16
  • @user I've added some code to cater for sensitivity to a period. Hopefully that will do what you want. – Bohemian Mar 31 '16 at 01:19
2

Some suggestions without code, since I'm betting that you can create your own code and do it well. Your logic within the for loop should include:

  • Split the String on white space -- you do this. Myself, I'd add wild cards to allow for one or more white-space chars, token.split("\\s+")
  • Check each token for terminal punctuation. -- there are many ways to do this including checking the last char in the token using token.charAt(token.length() - 1) == '.', or doing a token.contains("."). If you know you'll only be dealing with periods, this is simple to do. If you may have other punctuations, this can get more complex.
  • Remove and save the punctuation. If you know it will only be a period, this is simple. If it can be commas or more, this will get a little more complex.
  • If token is a key in the HashMap, replace it with text from HashMap. You already do this.
  • If terminal punctuation present, add it back and add the "Arr".
  • If no terminal punctuation present, add a space. " ".
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-1

Here is what you asked for (see my comments next to the source code):

package pirate;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class PirateTranslator {

    //declared a static constant containing the finishing word.
    private static final String ARR = " Arr.";

    public static void main(String[] args) {

        Map<String, String> hashmap = new HashMap<String, String>();
        hashmap.put("hello", "ahoy");
        ...
        hashmap.put("left", "port");

        Scanner scan = new Scanner(System.in);
        String token = scan.nextLine();
        String[] result = token.split("\\s");

        //use StringBuilder class for string concatenation
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < result.length; i++) {
            String word = result[i];

            //remove full stop from the input if exists
            if(word.endsWith("."))
            {
                word = word.substring(0, word.lastIndexOf("."));
            }

            if (hashmap.containsKey(word)) {

                //insert space in front of the next word except for the first and the last one.
                if(sb.length() > 0 && i < result.length - 1)
                {
                    sb.append(" ");
                }

                //translate and concatenate the next word
                sb.append(hashmap.get(result[i]));
            }
        }

        //don't include full stop after exclamation character
        if(sb.lastIndexOf("!") == -1 && sb.lastIndexOf(".") == -1)
        {
            sb.append(".");
        }

        //append final ' Arr.'
        sb.append(ARR);

        //print out the translated sentence.
        System.out.print(sb.toString());
    }
}
RZet
  • 914
  • 9
  • 11
  • 1
    Please don't spoon feed solutions to homework without explanation. What benefit to the OP is that? – Hovercraft Full Of Eels Mar 31 '16 at 00:12
  • you are right Hovercraft. There is no benefit. Thank you for your answer RZet. I appreciate your help but I do not want to have solutions (although yours is not correct:)). Just need explanations, so I can learn from it. – user5909208 Mar 31 '16 at 00:25
  • @user5909208 I corrected the case with full stop at the end of the input sentence. To explain my answer, it wasn't clear from your post you didn't expect a working code but some clues instead. – RZet Mar 31 '16 at 01:14