473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help!!! translate english sentence to pirate lingo...have it partially completed

ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways: 2d array and hashmap. both have the problem of translating a phrase with two words. im running out of time and need help bad.

HASHMAP WAY:
Expand|Select|Wrap|Line Numbers
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.awt.*;
  5. import java.util.*;
  6.  
  7. public class piratetalk
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         HashMap<String, String> h = new HashMap<String, String>(149, 0.75f);
  12.  
  13.  
  14.         h.put( "hello", "ahoy" );
  15.         h.put( "hi", "yo-ho" );
  16.         h.put( "pardon me", "avast" );
  17.         h.put( "excuse me", "arrr" );
  18.         h.put( "yes", "aye" );
  19.         h.put( "sir", "matey" );
  20.         h.put( "stranger", "scurvy dog" );
  21.         h.put( "your", "yer" );
  22.         h.put( "where", "whar" );
  23.         h.put( "you", "ye" );
  24.         h.put( "is", "be" );
  25.         h.put( "know", "be knowin" );
  26.         h.put( "far", "league" );
  27.         h.put( "coming", "comin" );
  28.         h.put( "friend", "mate" );
  29.         h.put( "hasn't", "not" );
  30.         h.put( "there has", "theres" );
  31.         h.put( "gathering", "gatherin" );
  32.         h.put( "are", "are ye" );
  33.  
  34.         Scanner scan = new Scanner(System.in);
  35.         System.out.println ( "Enter text you would like converted" );
  36.         String sentence = scan.nextLine();
  37.         String[] result = sentence.split("\\s");
  38.         for(int i = 0; i < result.length; i++){
  39.             if(h.containsKey(result[i])){
  40.                 result[i] = h.get(result[i]);
  41.             }
  42.             System.out.println(result[i]);
  43.         }
  44.     }
  45. }
2D ARRAY
Expand|Select|Wrap|Line Numbers
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.awt.*;
  5. import java.util.*;
  6.  
  7. public class piratetalkarraystyle
  8. {
  9.         public static void main(String[] args)
  10.         {
  11.             String[][] translateList = {{"hello", "ahoy"},
  12.                                         {"hi", "yo-ho" },
  13.                                             {"pardon me", "avast"},
  14.                                             {"yes", "aye"},
  15.                                             {"sir", "matey"},
  16.                                             {"are", "are ye"},
  17.                                             {"excuse me", "arrr"},
  18.                                            };
  19.  
  20.                 Scanner scan = new Scanner(System.in);
  21.                 System.out.println ( "Enter text you would like converted" );
  22.                 String sentence = scan.nextLine();
  23.                 String[] input = sentence.split("\\s");
  24.  
  25.                 for (int x=0; x<input.length; x++)
  26.                 {
  27.                     for (int y = 0; y < translateList.length; y++)
  28.                         if (input[x].equalsIgnoreCase(translateList[y][0]))
  29.                             System.out.println("True");
  30.                             //just checking ot make sure it works
  31.                 }
  32.         }
  33.  
  34.  
  35. }
can anyone fix this for me? keep in mind im new to java...been at this for quite a while
May 6 '07 #1
3 5582
JosAH
11,448 Expert 8TB
ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways: 2d array and hashmap. both have the problem of translating a phrase with two words. im running out of time and need help bad.
Using a HashMap is a good idea. You store either single words as the key
or two words separated by a single space, making up a single String again.

Think of a "String previous" word (initially it is null), that represents a previous
word that maybe can be translated together with a current word. If it can't
be translated together with the current word, the previous word will be output
untranslated and attempt to translate the current word on its own. If it can
be translated, output the translation, otherwise the current word becomes
the previous word. Take care to handle the previous word when the input
is reaches its end of file status.

kind regards,

Jos
May 6 '07 #2
Using a HashMap is a good idea.
My favorite translation website: Spanish to English
Aug 21 '17 #3
chaarmann
785 Expert 512MB
you should really take word boundaries in account. splitting by space is not enough.
Your rule far --> league would not be applied to "It is so far." because of the dot.

I would not split at all, it takes too much performance. And at the end you must assemble it back. Why not use regular expressions instead?
Expand|Select|Wrap|Line Numbers
  1. String sentence = "It is so far.";
  2. String result = sentence.replaceAll("\Wfar\W", "league")
Aug 21 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Julia | last post by:
I need help with architecture design,please: I have a server which constantly downloading messages from the internet and store them inside a data base. the server have two administrators...
3
by: Julia | last post by:
I need help with architecture design,please: I have a server which constantly downloading messages from the internet and store them inside a data base. the server have two administrators...
1
by: News | last post by:
I need help with confirm() funciton. I have an application that displays pages in English and French languages. What I need is to display buttons upon clicking confirm(), buttons for English...
4
by: enak | last post by:
I need help with a request that I have received. I have been asked to open up a new email message, populate all of the appropriate fields, add an attachment but I don't want to send the email. I...
10
by: ahoway | last post by:
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
3
by: karuchan | last post by:
Hello, it is my first time here and I am basically a beginner at programming PHP. I need help regrading PHP calling. Suppose I have a page named Index.php for a site and wanted to call a page...
1
by: jkincaid | last post by:
I need help on a simple database, I have limited ACCESS skills. Here's my dilemma: I have 2 tables: Table one is customer issues Table two is the particular customer Table one includes:...
3
by: ArchMichael | last post by:
i need help with vb in access. i have table call main_tbl and another table called details_tbl. main_tbl has the following fields issueID, text, PK PN, text description, text openby, text...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.