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: - import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Scanner;
-
import java.awt.*;
-
import java.util.*;
-
-
public class piratetalk
-
{
-
public static void main(String[] args)
-
{
-
HashMap<String, String> h = new HashMap<String, String>(149, 0.75f);
-
-
-
h.put( "hello", "ahoy" );
-
h.put( "hi", "yo-ho" );
-
h.put( "pardon me", "avast" );
-
h.put( "excuse me", "arrr" );
-
h.put( "yes", "aye" );
-
h.put( "sir", "matey" );
-
h.put( "stranger", "scurvy dog" );
-
h.put( "your", "yer" );
-
h.put( "where", "whar" );
-
h.put( "you", "ye" );
-
h.put( "is", "be" );
-
h.put( "know", "be knowin" );
-
h.put( "far", "league" );
-
h.put( "coming", "comin" );
-
h.put( "friend", "mate" );
-
h.put( "hasn't", "not" );
-
h.put( "there has", "theres" );
-
h.put( "gathering", "gatherin" );
-
h.put( "are", "are ye" );
-
-
Scanner scan = new Scanner(System.in);
-
System.out.println ( "Enter text you would like converted" );
-
String sentence = scan.nextLine();
-
String[] result = sentence.split("\\s");
-
for(int i = 0; i < result.length; i++){
-
if(h.containsKey(result[i])){
-
result[i] = h.get(result[i]);
-
}
-
System.out.println(result[i]);
-
}
-
}
-
}
2D ARRAY - import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Scanner;
-
import java.awt.*;
-
import java.util.*;
-
-
public class piratetalkarraystyle
-
{
-
public static void main(String[] args)
-
{
-
String[][] translateList = {{"hello", "ahoy"},
-
{"hi", "yo-ho" },
-
{"pardon me", "avast"},
-
{"yes", "aye"},
-
{"sir", "matey"},
-
{"are", "are ye"},
-
{"excuse me", "arrr"},
-
};
-
-
Scanner scan = new Scanner(System.in);
-
System.out.println ( "Enter text you would like converted" );
-
String sentence = scan.nextLine();
-
String[] input = sentence.split("\\s");
-
-
for (int x=0; x<input.length; x++)
-
{
-
for (int y = 0; y < translateList.length; y++)
-
if (input[x].equalsIgnoreCase(translateList[y][0]))
-
System.out.println("True");
-
//just checking ot make sure it works
-
}
-
}
-
-
-
}
can anyone fix this for me? keep in mind im new to java...been at this for quite a while
3 5582
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
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? - String sentence = "It is so far.";
-
String result = sentence.replaceAll("\Wfar\W", "league")
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |