Solved

Compare string array values

Posted on 2007-03-29
15
12,440 Views
Last Modified: 2010-05-18
Hi Experts

I am designing a MUD game and wanted to include a dance feature, something similar to dance dance revolution but for the keyboard. Unfortunately you cannot use keypress in a telnet session so I opted for an array of strings that are printed to the screen and the user has to type them back.

This is my code

public void dance(){

                        String input = null;
                        String[] word = new String[20];
                        String[] moves = {"up ","up "};
                        
                        for(int i=0; i< moves.length; i++){
                              Game.server.out(moves[i]);
                              try {
                                    input = Game.server.ir.readLine();
                                    word = input.split(" ");
                                    String out = moves[i];
                                    for(int j=0; j<word.length; j++){
                                    String in = word[j];
                                    if(in.equalsIgnoreCase(out)){
                                          Game.server.out("Cool");
                                    }
                                    }
                              } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                              }
                        try{
                        Thread.sleep(1000);
                        }
                        catch(Exception e)
                        {
                        System.out.println(e);
                        }
                        }                  
      }

It prints the array to the screen and accepts the incoming text but does not seem to compare the values tried all sorts can’t get it to work would appreciate any help.

What I would like is the word to print to the out stream the user enters it into the in stream and if it is right the out stream prints cool or something like that and then prints the next item in the array.

Thanks

Clive
0
Comment
Question by:clivecrawdevil
  • 9
  • 6
15 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18819492
Did you set auto-flush on true, while getting the outputstream?

You could also do:
Game.server.out("Cool");
Game.server.flush();

Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18819498
In case of auto-flushing you probably have sth like:
new PrintWriter(this.server.getOutputStream());

change that into:
new PrintWriter(this.server.getOutputStream(), true);

Mark
0
 

Author Comment

by:clivecrawdevil
ID: 18819689
Already had it flushing as you suggested in your second comment mark, I have also implemented flush() as advised in your first comment unfortunately it has no effect.

Does the code look correct? I haven't made any obvious errors?

Thanks for trying

Clive
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18819714
Looks ok i guess, it's not much code :-)
0
 

Author Comment

by:clivecrawdevil
ID: 18819742
Cheers really annoying just keep messing with it i guess!
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18819795
Maybe you typed the wrong word? :-)
If you could give me some code which I can easily compile and try then I can have a better look at it, for now as i see it, it looks like good code.

Mark
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 18819803
PS: wasn't kidding about the "did you type the correct word", i see a space after the "up", is that correct?

Mark
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:clivecrawdevil
ID: 18819841
You know what Mark your a legend! As soon as i took out the spaces it started to work.

Got another one for you shall i repost or submit it here?

Thanks again

Clive
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18819859
:-) haha, funny, well glad I got my glasses on. :-)

I don't mind, whatever you choose.
0
 

Author Comment

by:clivecrawdevil
ID: 18820136
I was hoping to put mulitiple arrays and have one choose at random is that easy to do? would i use math.random?

If it's tricky i will repost it for more points.

Thanks again
Clive
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18820155
That's easy i guess, you can use a 2 dimensional array or a list of arrays.
I'm not sure what you want to randomize, so let's say you have a fixed number of "random sequences", then you can put them all inside an array and pick a random element with:

int index = (int) Math.floor(Math.random()*somearray.length)
...somearray[index]...

Mark
0
 

Author Comment

by:clivecrawdevil
ID: 18820245
I was thinking of having multiple arrays such as

 String[] moves1 = {"up ","up "};
 String[] moves2 = {"up ","up "};
 String[] moves3 = {"up ","up "};

and then picking one of the arrays at random each time the code is executed.

Is your way a bit more elegant?

Thanks

Clive
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18820280
your way is not possible, since you cannot dynamically bind a number to a variable name like:

String[] randomMoves = (moves + Math.random());

So you need sth like;

String[][] moves= { {"up","left"}
                            ,  {"down","up"}
                            ,  {"up","right"} };

Now you can access each array with just an integer like:
moves[1] will get the array: {"down","up"}

Mark.
0
 

Author Comment

by:clivecrawdevil
ID: 18820302
Cool i understand thanks again Mark.

Hope you have a good day

Clive
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18820309
Always, good luck Clive.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now