Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner problem with "if"

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Czech rep.
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Beginner problem with "if"

    Greetings,
    I'm absolute beginner and I'm training a primitive program that ask you for a cookie and if you write "cookie" it shall thank you and if you write anything else, it'll be sad.
    But whatever I write, it still says me it's not a cookie..
    I'm a bit lost, cannot find anything on internet.. I'll be very glad for respond

    Code:
    import java.util.Scanner;
     
    class cookie {
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Insert cookie: ");
    		String temp = input.nextLine();
    		 if(temp == "cookie"){
    			 System.out.println("Thanks, nomnomnom");
    		 }else{
    			 System.out.println("Not a cookie. :(");
    		 }
    	}
    }
    Last edited by HakitoCZ; May 8th, 2013 at 10:41 AM. Reason: solved
    Non-native-english speaker, absolute beginner, on Xubuntu 13.04 w/ Eclipse.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner problem with "if"

    You need to use the equals() method to compare the contents of two Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    HakitoCZ (May 8th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Location
    Czech rep.
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner problem with "if"

    Yes, that's it! Thanks alot!
    Non-native-english speaker, absolute beginner, on Xubuntu 13.04 w/ Eclipse.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Books and tutorials for beginners in Java and Visual Basic
    By Professor Spider in forum The Cafe
    Replies: 5
    Last Post: April 9th, 2009, 10:57 AM