using any index of an array in a condition JAVA

Genkora

Frog blast the vent core!
Reaction score
92
I was wondering there was a simple way of using any index of an array in a conditional.

Like

if (String variable == array[any index])
{
do stuff
}

I'm making a text based adventure game and want to have multiple ways of saying commands like "move", like go, walk, travel, etc. So the player can type go north, or walk north, or travel north.
 

UndeadDragon

Super Moderator
Reaction score
447
I am not sure about in Java, but in other languages, the best way to do it would be to loop through all your array indicies, like:

Code:
foreach(var i = 0; i <= array.length; i++) {  
   if (String variable == array[i]) {
      do stuff
   }
}

NOTE: I do not know Java, this is a generic code that you should convert into the nearest way possible with Java.
 

Genkora

Frog blast the vent core!
Reaction score
92
ok, that makes sense. I'm having a problem though. No matter what the user inputs, it never makes the conditional true. Here is the bit of code with problems:

Code:
do
				{
					command = scan.nextLine();
					for (count = 0; count <= 5; count++)
					{
						if (command == move[count]+" "+N)
						{
							area = 1;
							numx = 1;
							count = 6;
						}
						else
						{
						System.out.println("I don't know what you mean by "+command);
						}
					}
				}
				while (numx == 0);
I don't know why it indented after the do so far.

move[count] being go, or move, or walk, etc., and N being "north". No matter what I type, it always comes out false. I tried using a println to compare move[count]+" "+N to the users input, and even if they come out the same, it always is false and I can't get out of the loop.
 

perkeyone

something clever
Reaction score
71
if (command == move[count]+" "+N)
should that line be
Code:
if (command == move[count]+" N")
?
i dont know java, so its just a guess.
i know in c++ it would think n is a variable name instead of a character
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
You can always use a Dictionary (or a Hashtable, the subclass of Dictionary):

Code:
import java.util.*;

// ....

Dictionary dict = new Dictionary();
dict.put("North",value);
dict.put("N",value);

// ....

if (dict.get(command) != null) {
    area = 1;
    numx = 1;
    count = 6;
} else {
    System.out.println("I don't know what you mean by "+command);
}

Useful if you want to associate commands with values. ;)
 

Genkora

Frog blast the vent core!
Reaction score
92
N is a variable containing "north". I don't understand why I can't use a conditional this way,
if the user inputs "go north" and it is equal to move[0]+" "+N (which printed out is "go north") it should be true. Except it isn't. And that dictionary thing is confusing me, I'm going to have many possible terms the user can input, and if they all have a value then the user could input "go south" to go north, at least that is what I'm seeing.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
N is a variable containing "north". I don't understand why I can't use a conditional this way,
if the user inputs "go north" and it is equal to move[0]+" "+N (which printed out is "go north") it should be true. Except it isn't. And that dictionary thing is confusing me, I'm going to have many possible terms the user can input, and if they all have a value then the user could input "go south" to go north, at least that is what I'm seeing.

I assumed from perkeyone's post that you actually meant to put in "N" and not the literal variable N. My example was showing that you could have multiple ways of saying "North" (such as "North", "N", "north", etc). You would of course put in different values for different locations/directions/objects.

I believe the reason your string comparisons aren't working correctly may be related to the fact that you must use the equals or equalsIgnoreCase method, like so:

Code:
if (command.equals(move[count]+" "+N))
{
...
}

However, a way to avoid that loop:

Code:
...

Dictionary moveAlias = new Dictionary();
moveAlias.put("move",value);
moveAlias.put("go",value);
moveAlias.put("head",value);

Dictionary adjacent = new Dictionary();
adjacent.put("north",north);
adjacent.put("south",south);
adjacent.put("dennis",dennis);

String[10] args = command.split(" ",0);

if (moveAlias.get(args[0]) != null) {
    if (adjacent.get(args[1]) != null) {
        ...
    } else {
        System.out.println(args[0] + " where?");
} else {
    System.out.println("I don't understand what you mean by " + args[0]);
}
...
I don't know why it indented after the do so far.

It's because you're using Hard Tabs for spacing, rather than spaces for tabs.
 

Genkora

Frog blast the vent core!
Reaction score
92
thanks, the command.equals() was it. I'm still not 100% sure on how to use dictionary, so for now I'm going to avoid it.
 
General chit-chat
Help Users

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top