Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Compare string array values

Posted on 2007-03-29
15
Medium Priority
?
12,529 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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