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
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top