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: IF with strings?[Solved]

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default IF with strings?[Solved]

    Hey, complete beginner programmer here, learnt about user input today and decided to mess about. All codes with integers and doubles worked perfectly, but I'm stuck with string. It shows up as incorrect when I write in "Yes", with or without brackets.

    I'm guessing there's something more to than just assigning "Yes" to the string ans variable.

    Any help would be appreciated!

     package MessingAbout;
     
    import java.util.Scanner;
     
    public class BlueSky {
    	public static void main(String[] args){
     
    		Scanner rocky = new Scanner(System.in);
     
    		String que, ans;
     
    		ans = "Yes";
     
    		System.out.println("Is the sky blue?");
     
    		que = rocky.nextLine();
     
    		if (que == ans) {
    			System.out.println("Correct!"); }
     
    		else {
    			System.out.println("Incorrect!"); }
     
    		}
    	}

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: IF with strings?

    Common error. Don't use the equality operator, "==", to compare Strings. Use the equals() method instead.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: IF with strings?

    Searched a few other threads, and found out that you have to use .equals() instead of == when comparing strings, but now I'm getting a syntax error with else.
    EDIT: Didn't see your post there Greg, I actually found out that you have to use equals when stumbling upon your response on another thread. But I'm still getting syntax error with else.

    package MessingAbout;
     
    import java.util.Scanner;
     
    public class BlueSky {
    	public static void main(String[] args){
     
    		Scanner rocky = new Scanner(System.in);
     
    		String que, ans;
     
    		System.out.println("Is the sky blue?");
     
    		que = rocky.nextLine();
     
    		if (que .equals("Yes") ); {
    			System.out.println("Correct!"); }
     
    		else {
    			System.out.println("Incorrect!") ; }
     
    		}
     
    		}

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: IF with strings?

    When you want help with errors, post them, copied and pasted into your post just as they appear at your end.

    Okay, this one's fairly easy. Remove the ':' at the end of the 'if' statement.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    import.Snupas (November 9th, 2013)

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: IF with strings?

    Thanks a bunch, it works perfectly now.

    Eclipse likes to point out that I'm missing a semicolon but not that I need to remove one.

Similar Threads

  1. Constructing Strings
    By Farmer in forum Loops & Control Statements
    Replies: 5
    Last Post: September 3rd, 2011, 10:24 AM
  2. [SOLVED] Strings and Characters
    By av8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 4th, 2011, 05:39 PM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM