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.
 
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.
 
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.
 
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
 
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. ;)
 
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.
 
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.
 
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
  • No one is chatting at the moment.
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      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