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 5 of 5

Thread: What's Wrong With My Code: If Statement.

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Exclamation What's Wrong With My Code: If Statement.

    	username = new JTextField(10);
    	add(username);

    	password = new JTextField(10);
    	add( password );

    	    	if(username.getText() = "_____" + password.getText() == "______" )


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: What's Wrong With My Code: If Statement.

    This is very vague to me. Could you post some guidance, as I don't really know what you want to do?

    Also, I did notice that this is problematic...
    if(username.getText() = "_____" + password.getText() == "______" )

    What are you doing here? The statement...
    username.getText() = "_____" + password.getText()
    ...will not work, as what you are doing here is telling Java to assign the value of "_____" + password.getText() to username.getText(), which is impossible. Methods do not work like that. Please, explain what you are trying to do, then I might be able to help better.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: What's Wrong With My Code: If Statement.

    There are several things that need to be changed:

    For "and" meaning "this as well as that" we use && not +. (+ is either an arithmetic operator meaning "added to", or a string concatenation operator meaning "joined on to". It already has enough to do!)

    = and == do totally different things. = assigns the value of the expression on the right hand side to the variable on the left hand side. ==, on the other hand means "is the same as". Except...

    When you are comparing objects, don't use ==. Use equals() instead. (== actually compares references values for equality, while .equals() is a method that any class can override to report whether objects should be considered the same. Don't worry about this - just use equals() to test for equality.)

    if(username.getText().equals("foo")) {
        // etc

    -----

    Refer to your textbooks etc for basic syntax. Or Oracle's Tutorial. If you can't understand a compiler message it pays to post both the code and the complete message. Generally these messages are very useful so it is worth finding out what they mean, rather than concentrating just on how to fix some specific problem.

  4. The Following User Says Thank You to pbrockway2 For This Useful Post:

    copeg (January 9th, 2012)

  5. #4
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: What's Wrong With My Code: If Statement.

    Quote Originally Posted by pbrockway2 View Post
    There are several things that need to be changed:

    For "and" meaning "this as well as that" we use && not +. (+ is either an arithmetic operator meaning "added to", or a string concatenation operator meaning "joined on to". It already has enough to do!)

    = and == do totally different things. = assigns the value of the expression on the right hand side to the variable on the left hand side. ==, on the other hand means "is the same as". Except...

    When you are comparing objects, don't use ==. Use equals() instead. (== actually compares references values for equality, while .equals() is a method that any class can override to report whether objects should be considered the same. Don't worry about this - just use equals() to test for equality.)

    if(username.getText().equals("foo")) {
        // etc

    -----

    Refer to your textbooks etc for basic syntax. Or Oracle's Tutorial. If you can't understand a compiler message it pays to post both the code and the complete message. Generally these messages are very useful so it is worth finding out what they mean, rather than concentrating just on how to fix some specific problem.
    Thanks, Solved .equals

  6. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: What's Wrong With My Code: If Statement.

    You're welcome.

Similar Threads

  1. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 06:28 PM
  2. What is wrong with my if/else statement?
    By CSUTD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 02:34 PM
  3. whats wrong with my if statement
    By semicolon in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2011, 04:28 PM
  4. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 08:59 AM
  5. what is wrong with my return statement??????
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 06:55 PM