?
Solved

Compare string array values

Posted on 2007-03-29
15
Medium Priority
?
12,518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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