Solved

Compare string array values

Posted on 2007-03-29
15
12,512 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

724 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