Solved

Compare string array values

Posted on 2007-03-29
15
12,485 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

837 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