Sunday, December 22, 2013

Text string not recognized


Hello all ! First post, first silly question. I am so very new to this, I just went through most of Java for Dummies.


My goal right now is to make my own homemade exercises. I have set myself to program a very simple text adventure (two rooms, simple commands, etc). And very early in the process here comes a hurdle.


Here's the source code so far.



Java Code:



import java.util.Scanner;


public class main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

// Variables
int game = 1; // keeps the game running
String Action; // what the player does

// Instances
Scanner myScanner = new Scanner(System.in);

// These are the different rooms, each room is a string array
// 0 = Full description
// 1 = Short description
// 2 = already been in room

String test[];
test = new String[3];
test[0] = "You are inside the white house. It is well furnished and lacking in furniture. There is a staircase going up.";
test[1] = "Inside the white house";
test[2] = "0";

// select the active room
String room[];
room = new String[3];
room = test;


// display room
while (game ==1) {
if (room[2] == "1") {System.out.println(room[1]);}
if (room[2] == "0") {System.out.println(room[0]) ; room[2] = "1";}
System.out.println("");
System.out.println("");
System.out.println("Your move ?");
Action = myScanner.nextLine();

//actions

if (Action == "look")
{room [2] = "1";
System.out.println("");
System.out.println(room[0]);
System.out.println("");
}

if (Action == "short")
{System.out.println("");
System.out.println(room[1]);
System.out.println("");}



}



}

}



What it is supposed to do is :

if i type "Look" I get the lengthy description of the room. If I type "short", I get the short description. (I know, it's a lousy game). The problem is that, well, it doesn't work. The program seems to be looping, displaying the short description after one run of the long one.

I seem to have found what the issue is, but cannot resolve it : the text string I input is not "recognized" by the "if" statement. I have tried to change "Action" into a int, and it seems to almost work provided I use numbers instead of the text commands.


What am I doing wrong ?


Thanks all the help in advance.



No comments:

Post a Comment