We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,612 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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(" ");
   }
   
  }
}
2
Contributors
1
Reply
1 Hour
Discussion Span
1 Year Ago
Last Updated
5
Views
nipur23
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
... trying to help
Moderator
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2854 seconds using 2.67MB