|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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();
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" );
}
Code:
String replacementWord = map.get(word);
if (replacementWord != null) {
// Word was found and should be replaced with replacementWord
// replace original word with pirate word
}
Code:
if ((sentence.toLowerCase().indexOf( " /* this is where you put the word your looking for but how do i include whole hashmap? */ ".toLowerCase()) != -1)
{
|
| Sponsored Links |
|
|
|
|||
|
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");
}
}
}
|
| Sponsored Links |
|
|
|
|||||
|
Quote:
Code:
System.out.println(result[i]); Code:
System.out.print(result[i] + " "); Quote:
Code:
if(i == 0){
System.out.print(result[i] + " ");
} else {
System.out.print(result[i].toLowerCase() + " ");
}
Quote:
![]() Last edited by John; 05-06-2007 at 01:16 AM. |
|
|||||
|
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 |
|
|||
|
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"
|
|
|||||
|
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') Code:
if(theString[0] >= 'a' && theString[0] <= 'z')
theString[0] -= 32;
|
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| 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 |