Results 1 to 2 of 2
  1. #1
    taxalot is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    753
    Rep Power
    2

    Default Re: Text string not recognized

    Always compare Strings using .equals() and not ==. So it would become: if ( Action.equals("short") ) {...}.

    I'll mention proper Java naming convention when you are a bit further in your learning curve :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 2
    Last Post: 12-24-2012, 03:22 AM
  2. text file into a string?
    By Landon in forum New To Java
    Replies: 5
    Last Post: 09-12-2010, 07:57 AM
  3. Text/String format..??
    By Neullson in forum New To Java
    Replies: 14
    Last Post: 07-23-2010, 08:51 AM
  4. get string from a text file
    By Kruptein in forum New To Java
    Replies: 11
    Last Post: 08-14-2009, 04:45 PM
  5. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •