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

Thread: String if-else not executing

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String if-else not executing

    hello,

    As you can see I am new to Java and would like some help with this problem. I've tried to solve it myself first but the issue that I'm having is I don't have the knowledge to know what I am doing wrong.

    // create a program that will say "Name" and then allow the useer's input of "their name" 
    // and then another response from the programme.
     
    import java.util.Scanner;
     
    public class HomeProject
    {
      public static void main(String[] args)
      {
    	 String yourName;
    	 String answer;
         String answer2 = "good, well have a nice day!";
    	 String answer3 = "unlucky, well have a nice day anyway!";
     
    	  Scanner input = new Scanner(System.in);
     
    	  System.out.println("What is your name?");
    	  yourName = input.nextLine();
    	  System.out.println();
     
    	  System.out.println( "Hello " + yourName + ", are you feeling well?" );
    	  answer = input.nextLine();
    	  System.out.println();
     
     
          if ("are you feeling well?".equals("yes"))
          {
          System.out.println(answer2);   
          answer2 = input.nextLine();
          System.out.println();
          }
     
          else if ("are you feeling well?".equals("no"))
              {
        	  System.out.println(answer3);   
              answer3 = input.nextLine();
              System.out.println();          }  
     
            input.close();
    	  }    
    }

    Any help will be appreciated and I'm sure its just something simple that I haven't properly understood.

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String if-else not executing

     if ("are you feeling well?".equals("yes"))
    The above compares two Strings that are not the same. It will never be true.
    Normally you compare the contents of a variable that has received a value from the user with one of the expected user inputs:
     input.equals("expected answer here")
    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:

    jimlumar` (December 13th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String if-else not executing

    Thank you, the code has been changed to input.

     
    import java.util.Scanner;
     
    public class HomeProject
    {
      public static void main(String[] args)
      {
    	 String yourName;
    	 String answer;
         String answer2 = "good, well have a nice day!";
    	 String answer3 = "unlucky, well have a nice day anyway!";
     
    	  Scanner input = new Scanner(System.in);
     
    	  System.out.println("What is your name?");
    	  yourName = input.nextLine();
    	  System.out.println();
     
    	  System.out.println( "Hello " + yourName + ", are you feeling well?" );
    	  answer = input.nextLine();
    	  System.out.println();
     
     
          if (input.equals("yes"))
          {
          System.out.println(answer2);   
          answer2 = input.nextLine();
          System.out.println();
          }
     
          else if (input.equals("no"))
              {
        	  System.out.println(answer3);   
              answer3 = input.nextLine();
              System.out.println();          }  
     
            input.close();
    	  }    
    }

    The code used to have input.equals... but from messing around with it I had made it worst. However it still seems like the 'if' and 'if else' statement is not being recognised and still isn't giving me "yes" = good, have a nice day or "no" = unlucky, have a nice day anyway.

    thank you for responding, do you have any ideas with what else I am doing wrong?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String if-else not executing

    What variable is the input from the user read into using the Scanner class's nextLine() method?

    The input variable contains the reference to a Scanner object.
    You need to compare the variable that holds the users input against the expected answers.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help: Executing perl script from java
    By jessie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 27th, 2012, 08:07 AM
  2. Program not executing correctly
    By daemonlies in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 1st, 2012, 11:14 PM
  3. executing a process in backhand
    By jack_nutt in forum Java IDEs
    Replies: 1
    Last Post: November 9th, 2011, 08:58 PM
  4. Executing Oracle Procedure
    By java_mein in forum JDBC & Databases
    Replies: 0
    Last Post: May 14th, 2010, 02:41 PM
  5. Problem with a waitFor() executing a script
    By Baldurian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 29th, 2010, 10:45 AM