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

Thread: If statement help

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If statement help

    I am trying to write an if statement based on input from a String that is dependant on the input from the user.

    import java.util.Scanner;
     
    public class FirstProject {
    	public static void main(String [] args){
    		Scanner input = new Scanner(System.in);
     
    		System.out.println ("Please tell me your name.");
    		String name = input.nextLine();
    		System.out.println ("Hello " + name + "!");
     
    		if (name = Ryan){
    			System.out.println ("Do the following please.");}
    		else{
    			System.out.println ("Please come back later.");}
    	}
    }

    As you can see I am trying to write an "if " statement based on the output for the "String name". Being that this entry is dependent on input from the user I can't specify in my "if" statement a static entry (name = Ryan) to continue. How would I go about doing this?


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    if(name.equalsIgnoreCase("Ryan"))

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    Thank you. This did resolve the issue but now I have another issue. I just just ran the program after adding the valid "if" identified above and it for some reason is printing out the "else" statement as well. I have pasted the results below. I though if the argument was satisfied in the "if" statement then the "else" was ignore but that doesn't seem to be the case. The only think that I have done differently is added an additional "if" statement prior to the "else" statement.

    import java.util.Scanner;
     
    public class FirstProject {
    	public static void main(String [] args){
    		Scanner input = new Scanner(System.in);
     
    		System.out.println ("Please tell me your name.");
    		String name = input.nextLine();
    		System.out.println ("Hello " + name + ("!"));
     
    		if (name.equalsIgnoreCase ("Ryan"))
    			{System.out.println ("Do the following.");}
    		if (name.equalsIgnoreCase ("Sarah"))
    			{System.out.println ("Do this.");}
    		else
    			{System.out.println ("Please come back later.");}
    	}}

    Please tell me your name.
    Ryan
    Hello Ryan!
    Do the following.
    Please come back later.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: If statement help

    I think you have to read more tutorials about statements
    use else if statement instead of second if

  5. #5

  6. The Following User Says Thank You to Sean4u For This Useful Post:

    LST (August 27th, 2011)

  7. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: If statement help

    im pretty new to java but i have been doing these for a while now so im getting good

    the way you have it right now is if if else so when you run it and the name ryan comes in it says *if ryan do this* so it does. then since you have another if statement right after it starts an *if sarah do this else do that* and since ryan was entered it wasnt sarah so it moved to the else...should be *if ryan do this else if sarah do this else do this...then its all 1 statement instead of 2

    i hope that all made some sense

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    Thank you everyone for your support. I found that tutorial that Sean4u posted as very helpful. I think I am sure I will figure it out from here and will re-post if I run into any other issues.

  9. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    In if condition you are using "=" it means that you are assinging value of name to Ryan. In this statment condition is not cheking. Change to "==" this is operator which is used to chek the values.

  10. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    You should never compare two Strings with "==". Always with <object>.equals(<other>) or variations on it.

    Your problem is that you're testing for two conditions that should have been related:
    if (x) a
    if (y) b
    else c

    Obviously, because "x" will fail the 2nd if, it will go on the else branch and do "c"
    The proper version should have been:
    if (x) a
    else if (y) b
    else c

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: If statement help

    Quote Originally Posted by LST View Post
    You should never compare two Strings with "==".
    Never say never. The are some occassions when you do need to compare objects with == to make sure you have the correct object. One example is using a single ActionListener attached to several buttons. You would use == to determine which button caused the event.
    Improving the world one idiot at a time!

  12. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: If statement help

    One thing just want to add. There is difference between If from C to if from Java. In C if(name = Ryan) would always result in true while the same statement will never compile in Java. Because in Java statement inside If should always be boolean and nothing else.
    So even if you write If(1) it will not compile in Java.

  13. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    public static void main(String [] args){
    Scanner input = new Scanner(System.in);

    System.out.println ("Please tell me your name.");
    String name = input.nextLine();
    System.out.println ("Hello " + name + "!");

    if (name =="Ryan"){
    System.out.println ("Do the following please.");}
    else{
    System.out.println ("Please come back later.");}
    }

  14. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement help

    Hi,


    You have written
    if (name.equalsIgnoreCase ("Ryan"))
    {System.out.println ("Do the following.");}
    if (name.equalsIgnoreCase ("Sarah"))
    {System.out.println ("Do this.");}
    else
    {System.out.println ("Please come back later.");}

    The 2 if statements are different and only one else you have written, that to second if.Because of this once the first if gets executed and prints the statement with in it as ithe condition satisfied and also it checks the next if, there the conditoin fails and it prints the corresponding else statement.

    To avoid this you can write your code as follows :

    if (name.equalsIgnoreCase ("Ryan"))
    {System.out.println ("Do the following.");}
    else if (name.equalsIgnoreCase ("Sarah"))
    {System.out.println ("Do this.");}
    else
    {System.out.println ("Please come back later.");}


    Thanks & Regards
    Divakara

Similar Threads

  1. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  2. If Statement in SQL String
    By Steffi1013 in forum Loops & Control Statements
    Replies: 0
    Last Post: March 30th, 2010, 03:25 PM
  3. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 06:57 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM