Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2013
    Location
    PA, USA
    Posts
    3
    Rep Power
    0

    Default Why is my if statement not working.

    Below is a part of the code and the problem isn't outside of the code (I don't think) whenever I run this and I type "stopwatch" or whatever I put the user input too it doesn't work. I use Eclipse as my IDE.
    Java Code:
     
            System.out.println("Select an option:" + "\n1. Stopwatch");
            String user_Input = sc.nextLine();
    		
            if(user_Input == "stopwatch") {
    		System.out.println(user_Input + "It works!");
    		int timePassed = 0;
    		while(timePassed > -1) {
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			++timePassed;
    			System.out.println(timePassed);
    				
    		}
    			
    	}else{
    		System.out.println("Error.");
    			
    	}
    Last edited by OverRatedProgrammer; 08-23-2013 at 01:17 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Why is my if statement not working.

    Use equals() and not == to compare strings.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Jul 2013
    Location
    PA, USA
    Posts
    3
    Rep Power
    0

    Default Re: Why is my if statement not working.

    how exactly would I do that can you tell me like how I would include that in my code? I did if(user_Input equals() "stopwatch") doubt that's what you meant

  4. #4
    Join Date
    Jul 2013
    Location
    PA, USA
    Posts
    3
    Rep Power
    0

    Default Re: Why is my if statement not working.

    nvm I figured it out variable.equals("test"){ } thx man!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Why is my if statement not working.

    You're Welcome!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Java if else statement not working
    By Spiritwo in forum New To Java
    Replies: 3
    Last Post: 07-17-2012, 11:28 PM
  2. Why isn't this if statement working?
    By ocomobock in forum New To Java
    Replies: 3
    Last Post: 03-27-2012, 03:13 AM
  3. 'if' statement not working?
    By Etimer in forum New To Java
    Replies: 4
    Last Post: 03-10-2012, 06:18 AM
  4. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  5. Update statement not working.
    By OMFGITSROHIT in forum JDBC
    Replies: 5
    Last Post: 04-08-2010, 02:03 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
  •