1,105,546 Community Members

Basic Java Translator using HashMap

Member Avatar
nipur23
Newbie Poster
1 post since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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(" ");
   }
   
  }
}
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: