Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ignoring words if not contained in array.  (Read 1469 times)
0 Members and 1 Guest are viewing this topic.
Offline Hauk

Junior Member




Guitar, Norse Mythology, java and women=life


« Posted 2005-12-06 23:05:23 »

Hi guys. Bit of a problem from me again. This time its not homework cos its not wednesday  Wink Its an exercise i found on the net  Tongue

But what I'm wondering is, how do i get a program to ignore a word(i.e String) if its not contained in an array.

Here's what I'm working on:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public class PirateTranslater
{
   static String TranslateToPirate(String userWord)
   {

      String translatedForBlackBeard = "";

      String space = " ";

      String [] pirateVocabulary = {"ahoy",  "yo-ho-ho", "avast", "arrr", "me", "me bucko", "matey", "proud beauty", "comely wench", "scurvy dog", "whar", "be", "th'", "ye", "be tellin'", "be knowin'", "how many leagues", "barnacle-covered", "comely", "grog-filled", "broadside", "head", "galley", "fleabag inn", "Skull & Skuppers", "buried treasure"};

      String [] userText = {"hello", "hi",  "pardon me", "excuse me", "my", "friend", "sir", "madam", "miss", "stranger", "where", "is", "the", "you", "tell", "know", "how far", "old", "attractive", "happy", "nearby", "restroom", "restaurant", "hotel", "pub", "bank"};

         for(int i = 0; i < userText.length; i++)
         {

            if(userWord.equals(userText[i]))
            {
               translatedForBlackBeard = pirateVocabulary[i];  
           
            }
            else
            {
               return userWord ;
            }
         }

         return translatedForBlackBeard;
     
   }

   public static void main(String [] args)
   {
      System.out.println("Say somethin' to ol' BlackBeard!");

      String word = Console.readToken();

      while(word != null)
      {

         String convertedWord = TranslateToPirate(word);

         // deal with the word
        System.out.print(convertedWord + " ");

         //read in the next word
        word = Console.readToken();      
      }
   }
}


Here's the problem. If i remove the else statement in the method, it prints out all the words that can be translated(i.e those contained in userText) but doesn't print the ones that aren't contained in the method, i.e such as 'there' and 'could'. But if i do include the else staement, it just returns the original string. So any thoughts from you guys??

By the way. I'm using IO redirection for it, and here's the input in a .txt file:

1  
2  
hello there sir
could you tell me where is the nearest restaurant


Thats my input!

And again, I stress its NOT homework, I'm just trying to build my skills!

And I appreciate everything you guys do for me!

Thx

Hauk

I want to become a hermit and live in the harsh wastelands of Norway. I want to have a cloak made of bearskin and hunt the demons of the night.

I was born for a reason and am not about to lie down and die like the rest of you. Anybody with me?
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2005-12-06 23:13:01 »

If I understand your problem, you want to "tokenize" a string. (i.e. Convert a string of text into an array of strings that can be processed.) Look up "java.util.StringTokenizer" as well as "java.lang.String.split()". Between the two of those you should be able to figure out a solution. Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Hauk

Junior Member




Guitar, Norse Mythology, java and women=life


« Reply #2 - Posted 2005-12-06 23:15:17 »

If I understand your problem, you want to "tokenize" a string. (i.e. Convert a string of text into an array of strings that can be processed.) Look up "java.util.StringTokenizer" as well as "java.lang.String.split()". Between the two of those you should be able to figure out a solution. Smiley

hmm, I'll give it a look up and post back here. And I remember you from my days of lurking here  Grin

Hauk

I want to become a hermit and live in the harsh wastelands of Norway. I want to have a cloak made of bearskin and hunt the demons of the night.

I was born for a reason and am not about to lie down and die like the rest of you. Anybody with me?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hauk

Junior Member




Guitar, Norse Mythology, java and women=life


« Reply #3 - Posted 2005-12-06 23:28:07 »

Actually now that I think of it, I think you misunderstood me. It doesn't read in the entire sentence as a string, it reads in each individual word and treats it as a string. Sorry if i wasn't clear enough!

Any other ideas now that i've cleared that up?

Thanks jbanes!

Hauk

I want to become a hermit and live in the harsh wastelands of Norway. I want to have a cloak made of bearskin and hunt the demons of the night.

I was born for a reason and am not about to lie down and die like the rest of you. Anybody with me?
Offline cborders

Junior Member





« Reply #4 - Posted 2005-12-07 00:06:00 »

What about ArrayLists?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
String list[];
ArrayList<String> userText = new ArrayList<String>();
ArrayList<String> pirateVocabulary = new ArrayList<String>();

list = new String[] {"hello", "hi",  "pardon me", "excuse me", "my", "friend", "sir", "madam", "miss", "stranger", "where", "is", "the", "you", "tell", "know", "how far", "old", "attractive", "happy", "nearby", "restroom", "restaurant", "hotel", "pub", "bank"};
for(int i = 0; i < list.length; i++)
{
   userText.add(list[i]);
}

list = new String[] {"ahoy",  "yo-ho-ho", "avast", "arrr", "me", "me bucko", "matey", "proud beauty", "comely wench", "scurvy dog", "whar", "be", "th'", "ye", "be tellin'", "be knowin'", "how many leagues", "barnacle-covered", "comely", "grog-filled", "broadside", "head", "galley", "fleabag inn", "Skull & Skuppers", "buried treasure"};
for(int i = 0; i < list.length; i++)
{
   pirateVocabulary.add(list[i]);
}

System.out.println(userText.indexOf(testString) > 0 ? pirateVocabulary.get(userText.indexOf(testString)) : testString);

That last line says if the index of the testString is not negative, print out the corresponding word from the PirateVocabulary, or else return the original string
Offline Hauk

Junior Member




Guitar, Norse Mythology, java and women=life


« Reply #5 - Posted 2005-12-07 00:39:22 »

I see what you mean there, but is there a way of making it into simpler code? Without arrayLists? I really don't want to jump in the deep end if you get my meaning  Undecided

Hauk

I want to become a hermit and live in the harsh wastelands of Norway. I want to have a cloak made of bearskin and hunt the demons of the night.

I was born for a reason and am not about to lie down and die like the rest of you. Anybody with me?
Offline cylab

JGO Knight


Medals: 34



« Reply #6 - Posted 2005-12-07 09:45:50 »

Inside your for loop you check whether the userWord is equal to the current indexed element inside userText. Unfortunatly the loop does not stop, when the correct translation is found, so in the next iteration, this check fails and returns the original word immediately (although there is a correct translation in translatedForBlackBeard).

I see what you mean there, but is there a way of making it into simpler code? Without arrayLists? I really don't want to jump in the deep end if you get my meaning Undecided

In my eyes the ArrayList solution is much simpler as your code  Tongue

However I would suggest to use a HashMap for this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
public class PirateTranslator
{
   private static HashMap translations;
   static
   {
      translations= new HashMap();
      translations.put("hello","ahoy");
      translations.put("hi","yo-ho-ho");
      translations.put("pardon me","avast");
      translations.put("excuse me","arrr");
      translations.put("my","me");
      translations.put("friend","me bucko");
      translations.put("sir","matey");
      translations.put("madam","proud beauty");
      translations.put("miss","comely wench");
      translations.put("stranger","scurvy dog");
      translations.put("where","whar");
      translations.put("is","be");
      translations.put("the","th'");
      translations.put("you","ye");
      translations.put("tell","be tellin'");
      translations.put("know","be knowin'");
      translations.put("how far","how many leagues");
      translations.put("old","barnacle-covered");
      translations.put("attractive","comely");
      translations.put("happy","grog-filled");
      translations.put("nearby","broadside");
      translations.put("restroom","head");
      translations.put("restaurant","galley");
      translations.put("hotel","fleabag inn");
      translations.put("pub","Skull & Skuppers");
      translations.put("bank","buried treasure");
   }

   static String TranslateToPirate(String userWord)
   {
      // Is there a translation? then return it
     if(translations.containsKey(userWord)) return (String)translations.get(userWord);
      // Return the original word as fallback
     return userWord;
   }
}


One note: the translation of "pardon me" and "excuse me" will not work, if you feed single words to the method, so you might want to create a line translater, which might be challenging Shocked
Another note: don't use upper case names for methods/functions in java, they are considered to be reserved for classes only Wink

Mathias - I Know What [you] Did Last Summer!
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #7 - Posted 2005-12-07 19:16:56 »

I see what you mean there, but is there a way of making it into simpler code? Without arrayLists? I really don't want to jump in the deep end if you get my meaning  Undecided

Sorry, I misunderstood what you needed. You'll forgive me if I say so, but your problem is so simple that it didn't even occur to me what you were asking. Let's try this again, shall we?

So your problem is that this code returns the original text:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
static String TranslateToPirate(String userWord)
{
  String translatedForBlackBeard = "";

  String space = " ";

  String [] pirateVocabulary = {"ahoy",  "yo-ho-ho", "avast", "arrr", "me", "me bucko", "matey", "proud beauty", "comely wench", "scurvy dog", "whar", "be", "th'", "ye", "be tellin'", "be knowin'", "how many leagues", "barnacle-covered", "comely", "grog-filled", "broadside", "head", "galley", "fleabag inn", "Skull & Skuppers", "buried treasure"};

  String [] userText = {"hello", "hi",  "pardon me", "excuse me", "my", "friend", "sir", "madam", "miss", "stranger", "where", "is", "the", "you", "tell", "know", "how far", "old", "attractive", "happy", "nearby", "restroom", "restaurant", "hotel", "pub", "bank"};

  for(int i = 0; i < userText.length; i++)
  {
    if(userWord.equals(userText[i]))
    {
      translatedForBlackBeard = pirateVocabulary[i];
    }
    else
    {
      return userWord ;
    }
  }

  return translatedForBlackBeard;
}


But if you remove the "else { return userWord; }" it returns only Pirate speak, right?

The answer is simple. Set "translateForBlackBeard" to equal "userWord" before you begin looping, then delete the "else" statement.

Consider the following code:

1  
2  
3  
4  
5  
6  
7  
private String[] ENGLISH = {"hello", "goodbye"};
private String[] SPANISH = {"hola", "adios"};

public String translate(String word)
{
    return word;
}


What does it do? Does it change the word passed in at all?

Now consider this version:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private String[] ENGLISH = {"hello", "goodbye"};
private String[] SPANISH = {"hola", "adios"};

public String translate(String word)
{
    for(int i=0; i<ENGLISH.length; i++)
    {
        if(word.equalsIgnoreCase(ENGLISH[i])) return SPANISH[i];
    }

    return word;
}


Does this version change the word? What happens if the word is not in the dictionary? Does it continue testing words after it finds a match?

As for the ArrayLists, don't fear them. They are extremely powerful and can help you a great deal. You would be wise to learn them. Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Hauk

Junior Member




Guitar, Norse Mythology, java and women=life


« Reply #8 - Posted 2005-12-07 23:18:17 »

I could kiss you!!!!!!

Thanks it works great!

You rock man! This place has never failed me EVER!

A very happy,

Hauk   Grin

I want to become a hermit and live in the harsh wastelands of Norway. I want to have a cloak made of bearskin and hunt the demons of the night.

I was born for a reason and am not about to lie down and die like the rest of you. Anybody with me?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!