Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Input in an if statement
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
username123




PostPosted: Wed Sep 23, 2015 8:07 am   Post subject: Input in an if statement

I can't get this code to work:

Java:

String answer;
   
    System.out.println("Do you have $200,000 - $500,000 or more?");
    answer = input.next();
   
    if (answer == "yes")
    {
      System.out.println("Do you want to get to places quickly?");
      answer = input.next();
     
      if (answer == "yes")
      {
        System.out.print("Purchase a Lamborghini Aventador.");
      }
      else if (answer == "no")
      {
        System.out.print("Purchase a Rolls-Royce Phantom.");
      }
      else
      {
        System.out.print("Invalid answer.");
      }
    }
    else if (answer == "no")
    {
      System.out.println("Do you have $100,000 - $200,000?");
      answer = input.next();
     
      if (answer == "yes")
      {
        System.out.println("Do you like imports?");
        answer = input.next();
       
        if (answer == "yes")
        {
          System.out.print("Purchase a Nissan GTR.");
        }
        else if (answer == "no")
        {
          System.out.print("Purchase a Dodge Viper SRT.");
        }
        else
        {
          System.out.print("Invalid answer.");
        }
      }
      else if (answer == "no")
      {
        System.out.print("Purchase a Honda Fit.");
      }
      else
      {
        System.out.print("Invalid answer.");
      }
    }
    else
    {
      System.out.print("Invalid answer.");
    }


Whenever I run it, it displays the first question and asks for input, but no matter what I enter, it always outputs "Invalid answer." and stops there. How do I fix this?
Sponsor
Sponsor
Sponsor
sponsor
username123




PostPosted: Wed Sep 23, 2015 8:37 am   Post subject: Re: Input in an if statement

Nevermind, I figured it out. (replaced == with .equals)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: