954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic Java Translator using HashMap

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(" ");
   }
   
  }
}
nipur23
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: