Results 1 to 5 of 5
  1. #1
    topps is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Help with my traffic light program

    When I type in 'green' at the prompt 'Enter your traffic light colour:', 'GO' is not output. Can anyone help.

    Java Code:
    package testing;
    import java.util.Scanner;
    
    public class trafficlight {
    
    	public static void main(String[] args) {
    		
    		Scanner Scanner1 = new Scanner (System.in);
    		System.out.println("Enter your traffic light colour:");
    		String userinput = Scanner1.nextLine();
    		
    		//Testing the user input from the Scanner
    		if (userinput == "green"){
    			System.out.println("GO");}
    		
    		else if  (userinput == "amber"){
    			System.out.print("GET READY");}
    		
    		else if  (userinput == "red"){
    			System.out.print("STOP");}
    		
    		}
    	}
    Last edited by Eranga; 04-09-2010 at 02:55 PM. Reason: added code tags

  2. #2
    askinne2 is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    You need to use this code instead of the one you have...

    Java Code:
    if (userinput.equals("green")){
    System.out.println("GO");}

  3. #3
    topps is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Thank you for your help

    Thanks for your help with that code askinne2, it works perfectlly.

  4. #4
    askinne2 is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    No problem sir. Goodluck with the rest of your semester.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by topps View Post
    Thanks for your help with that code askinne2, it works perfectlly.
    When you start to dig into Java in details, study about difference between == and equals(). Good luck with your studies.

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Generate IP/Ethernet traffic
    By vincent2001@gmail.com in forum New To Java
    Replies: 0
    Last Post: 08-22-2008, 10:08 PM
  3. Traffic simulator
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 03:59 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Heavy and light component mixing
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 11:23 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
  •