1.11M Members

Basic Java Translator using HashMap

 
0
 

Hi guys,
I'm almost finished my assignment. It's purpose is to translate english words to pirate language. For example, when some enters "hello", "ahoy" is returned. But when I enter two words like "pardon me", "null" is returned even though I have assigned "avast" to it. I'm thinking this is because I have included Console.readToken() instead of readString(). But token in my opinion works better. So I'm not sure what I can do for more than one word like "pardon me" or "excuse me". Can anyone help?

Here is the code:

import java.util.*; 
public class PirateTranslator
{
  public static void main(String [] args)
  {
   HashMap <String, Object> hashmap = new HashMap <String, Object>();
   hashmap.put("hello", "ahoy");                                        
   hashmap.put("madam", "proud beauty");
   hashmap.put("hi", "yo-ho-ho");
   hashmap.put("pardon me", "avast");
   hashmap.put("excuse me", "arrr");
   hashmap.put("my", "me");
   hashmap.put("friend", "me bucko");
   hashmap.put("sir", "matey");
   hashmap.put("madam", "proud beauty");
   hashmap.put("miss", "comely wench");
   hashmap.put("stranger", "scurvy dog");
   hashmap.put("where", "whar");
   hashmap.put("is", "be");
   hashmap.put("the", "th'");
   hashmap.put("you", "ye");
   hashmap.put("tell", "be tellin'");
   hashmap.put("know", "be knowin'");
   hashmap.put("how far", "how many leagues");
   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("pub", "Skull & Scuppers");
   hashmap.put("bank", "buried treasure");
   
   while(!Console.endOfFile())
   {
   
     String input = (String) hashmap.get(Console.readToken());
   
     System.out.print(input);
     System.out.print(" ");
   }
   
  }
}
 
0
 

"But token in my opinion works better"
Obviously not. If the input can be multiple words separated by blanks then either you have to loop reading all the tokens on the line then concatenate them again, or just read the whole line.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: