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: String and IF statement

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String and IF statement

    	String user1 = JOptionPane.showInputDialog("Please enter the first player's name"); //Ask's for the user's name
    	String user2 = JOptionPane.showInputDialog("Please enter the second player's name"); //Ask's for the user's name
    	JOptionPane.showMessageDialog(null, user1 + " will go first! good luck!");

    I have this code and I wanted to make it so that if the user did not enter any name, it'd set the user1 to player1 and user2 to player2.

    I tried using an if statement something like

    if(user1 == " ")
    {
    }
    but not sure what to put within the if statement


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String and IF statement

    Use equals() instead of == to compare object values. See http://www.javaprogrammingforums.com...html#post18725

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String and IF statement

    Ok thanks, i've fixed it

Similar Threads

  1. [SOLVED] Please help me with my String variable and switch statement problem.
    By ace1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 7th, 2013, 09:15 PM
  2. String API: Converting a given statement
    By thiduck in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2013, 02:51 AM
  3. Switch statement using a String with Java 1.7
    By eloel33 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 23rd, 2012, 05:11 AM
  4. Help with Loop Structures, If-else statement, and String
    By javabeg123 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2011, 10:01 PM
  5. If Statement in SQL String
    By Steffi1013 in forum Loops & Control Statements
    Replies: 0
    Last Post: March 30th, 2010, 03:25 PM