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( "we are", "we be" );
Scanner scan = new Scanner(System.in);
System.out.println ( "Enter text you would like converted" );
String sentence = scan.nextLine();
here is the problem. i have no idea how to loop through the sentence...find words that match the hashmap, replace words, then output the final result. i started off by using StringTokenizer:
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 x=0; x<result.length; x++)
{
if (result[x] == h.containsValue ())
System.out.println( "TRUE" );
}
but couldn't get that to work. then i thought to do something like this:
String replacementWord = map.get(word);
if (replacementWord != null) {
// Word was found and should be replaced with replacementWord
// replace original word with pirate word
}
then i got lost again. tried this:
if ((sentence.toLowerCase().indexOf( " /* this is where you put the word your looking for but how do i include whole hashmap? */ ".toLowerCase()) != -1)
{
nope, nothing. can anyone help me out? please keep in mind im very new to java. this is driving me insane. its such a simple concept and yet i cant make it work.


Sign In
Create Account

Back to top









