Results 1 to 2 of 2
  1. #1
    MutatedGamers is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default I cannot get my if statement to work very well.

    Here is my code:
    Java Code:
    package net.TextGame;
    
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferStrategy;
    import java.util.Scanner;
    
    import javax.swing.JFrame;
    
    public class Display extends Canvas implements Runnable {
    
    	public static final int GAMEHEIGHT = 600;
    	public static final int GAMEWIDTH = 600;
    	public boolean running;
    	Scanner UserInput = new Scanner(System.in);
    
    	public void run() {
    		System.out.println("Level one : Paris");
    
    		System.out.println("Your identity in this mission is Carvon lorez");
    
    		System.out.println("He is thirty four years old.");
    
    		System.out.println("Short black hair.");
    
    		System.out.println("You are on holiday in paris, on your own.");
    
    		System.out.println("You have left your wife and two children, Sam and harvey back in england.");
    
    		System.out.println("Your mission is to find ROBERT WILLIAM FISHER and neutralise him");
    
    		System.out.println("He killed all of his family and then blew up their house.");
    
    		System.out.println("Your flight leaves tomorrow at 9:00 GMT");
    
    		System.out.println("Oh and remember dont get killed.");
    		System.out.println("");
    		System.out.println("");
    		System.out.println("");
    		System.out.println("");
    		System.out.println("");
    
    		System.out.println("                Paris Airport           ");
    
    		System.out.println("You can see lots of french people waving signs around they all have names on.");
    
    		System.out.println("You have two options:");
    
    		System.out.println("one. Get a taxi from outside the airport");
    
    		System.out.println("two. Take someone elses taxi by faking about your name.");
    
    		System.out.println("...Type the number of which option you choose...");
    		System.out.println("eg. one,two. This will be the same for all Questions throughout the game.");
    
    		String taxi1 = UserInput.nextLine();
    
    		if (taxi1 == one) {
    			System.out.println("It worked");
    
    		}
    		else if (taxi1 == two) 
    		{
    			System.out.println("It worked 2");
    		}
    		
    		else 
    		{
    			System.out.println("Did you enter it wrong? Start again!");
    		}
    
    	}
    
    	public static void main(String args[]) {
    		new Display().start();
    
    	}
    
    	private void start() {
    		Thread t = new Thread(this);
    		t.setPriority(Thread.MAX_PRIORITY);
    		t.start();
    	}
    
    }
    the bit with the error is here :
    Java Code:
    System.out.println("You can see lots of french people waving signs around they all have names on.");
    
    		System.out.println("You have two options:");
    
    		System.out.println("one. Get a taxi from outside the airport");
    
    		System.out.println("two. Take someone elses taxi by faking about your name.");
    
    		System.out.println("...Type the number of which option you choose...");
    		System.out.println("eg. one,two. This will be the same for all Questions throughout the game.");
    
    		String taxi1 = UserInput.nextLine();
    
    		if (taxi1 == one) {
    			System.out.println("It worked");
    
    		}
    		else if (taxi1 == two) 
    		{
    			System.out.println("It worked 2");
    		}
    		
    		else 
    		{
    			System.out.println("Did you enter it wrong? Start again!");
    		}
    It sais that one and two cannot be resolved to variable. But the user will enter one or two and i need to to branch off a then do the alternate paths. but because it is not a variable it wont work. Basicaly im trying to get the string of Taxi1 to be equaly to on or two and then do the code in the if statement.
    Help is appreciated.
    MutatedGamer

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: I cannot get my if statement to work very well.

    Literal Strings should be between double quotes, so "one" and "two"; also, you can't compare two Strings for equality with the == operator; use the equals( ... ) method for that; so e.g. taxi1.equals("one").

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Can't get Update statement to work
    By Reptar693 in forum JDBC
    Replies: 8
    Last Post: 09-01-2012, 10:36 AM
  2. Replies: 4
    Last Post: 04-20-2012, 03:17 PM
  3. Replies: 6
    Last Post: 03-31-2011, 03:33 PM
  4. Replies: 3
    Last Post: 02-23-2011, 07:25 PM
  5. SQL statement doesn't work....
    By pbaudru in forum New To Java
    Replies: 4
    Last Post: 04-08-2010, 09:51 PM

Posting Permissions

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