Jump to content

need help writing prog to convert english sentence to pirate lingo

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
yeah i know it sounds lame but i got to finish this. basically i decided to go with hashmap to keep track of all the translations. so basically the first part looks like this:

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.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You're on the right track. Essentially what you want to do is compare the values in the result's array to the values in the hashmap. If there is a match, replace that result with the pirates equivalent. The one problem I see though is that you will run into problems with phrases such as "pardon me" because the results array would list those as two separate elements, and when you check to see if the hashmap contains those elements it will return false because the hashmap does not contain "pardon" or "me." Here is what I think you were trying to accomplish, but note the bug is not worked out.

		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]);
		}

And speaking of pirates, I cant wait until May 25th to see Pirates of the Caribbean 3!

#3
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
ok now here is the 64k dollar question:

how can i output it so that eachword doesn't have its own line and if someone was to say Hello there it would output Ahoy there (note the caps in first word)

#4
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
ahh i see your point with the two word phrases part. hmm, any bright ideas on how to fix it?

#5
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
i rewrote it using arrays:

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");

				}

		}



}

but again i dont' know how to replace the values with pirate code and output it. also it still has problem with using 2 word phrases...anyone plz!

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

dalearyous said:

ok now here is the 64k dollar question:

how can i output it so that eachword doesn't have its own line
Change the
System.out.println(result[i]);
to
System.out.print(result[i] + " ");



dalearyous said:

if someone was to say Hello there it would output Ahoy there (note the caps in first word)
Not 100% sure what you mean but if I understand correctly, you could just change all first letters in your hashmap to capital letters, then while it is printing out, do a simple if - else. Something like

if(i == 0){
System.out.print(result[i] + " ");
} else {
System.out.print(result[i].toLowerCase() + " ");
}

dalearyous said:

ahh i see your point with the two word phrases part. hmm, any bright ideas on how to fix it?
Not at the moment :)

#7
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
System.out.print(result[i] + " ");

that doesn't work just adds space to the end of each result..the output still has one word on each line

#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I'm not sure what IDE you are using but the print ( not the println ) command will print everything to the same line (at least with every IDE and Linux shell I've worked with)

http://img248.images.../consoleeb4.jpg

#9
dalearyous

dalearyous

    Newbie

  • Members
  • Pip
  • 9 posts
yeah it works fine, didn't see the println not being there. for the capitalization basicallly if a match occurs and the first letter in the phrase is to be replaced is uppercase, then the first letter in the substituted pirate phrase is likewise capitalized: "Sir excuse me" ==> "Matey arrr"

#10
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
There's no standard function for doing so, but it isn't hard to do manually. You first have to check if the first character is a small character.
if(theString[0] >= 'a' && theString[0] <= 'z')
If it's true then you can use some function to convert the single character to uppercase. You can of course also do this by yourself, simply by subtracting 32 from the character.
if(theString[0] >= 'a' && theString[0] <= 'z')
    theString[0] -= 32;


#11
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Two things regarding the above post in Java:
To check the first letter in Java you can use the charAt(0) method on a String.

And if you were to subtract 32 from a character, you would have to recast it to a char because

'a'-32 = 64
(char)64 = A

#12
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It was a pseudocode.
But thanks for putting the right functions, and casting in.