Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-2007, 08:33 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default need help writing prog to convert english sentence to pirate lingo

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:

Code:
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:

Code:
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:

Code:
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:

Code:
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-05-2007, 09:23 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

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.

Code:
		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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-05-2007, 10:38 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-05-2007, 10:40 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

ahh i see your point with the two word phrases part. hmm, any bright ideas on how to fix it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-06-2007, 12:44 AM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

i rewrote it using arrays:

Code:
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-06-2007, 12:51 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Quote:
Originally Posted by dalearyous View Post
ok now here is the 64k dollar question:

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


Quote:
Originally Posted by dalearyous View Post
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

Code:
if(i == 0){
System.out.print(result[i] + " ");
} else {
System.out.print(result[i].toLowerCase() + " ");
}
Quote:
Originally Posted by dalearyous View Post
ahh i see your point with the two word phrases part. hmm, any bright ideas on how to fix it?
Not at the moment

Last edited by John; 05-06-2007 at 01:16 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-06-2007, 02:18 AM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-06-2007, 02:56 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

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.imageshack.us/img248/8483/consoleeb4.jpg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-06-2007, 03:47 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

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"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-07-2007, 12:58 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
Code:
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.
Code:
if(theString[0] >= 'a' && theString[0] <= 'z')
    theString[0] -= 32;
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 11:33 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads