#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    New Mexico, USA
    Posts
    2
    Rep Power
    0

    Error in my java program, need help.


    Hey guys, Im a java noob, and I need help for a class I'm taking.

    With this code I'm trying to ask the reader if they are a pirate, and if they answer "yes" it will print out a positive response, and if they print anything other than "yes" it will print a negative response. I use an If/Else statement, yet for some reason, the user imput (AskPirate) is not equal to "yes" even when I type in "yes". Here it is (formatting is a little off due to moving this from "Eclipse":

    import java.util.Scanner;
    public class umightbapirateif {

    /**
    * @param args
    */
    public static void main(String[] args) {
    //Initial question
    System.out.println("Are you a pirate?");
    //Scanner
    Scanner pirate = new Scanner(System.in) ;
    String AskPirate = pirate.next();

    if
    Code:
    (AskPirate == "yes")
    {
    //yes
    System.out.println("Aye I knew you were maytee!!");
    }
    else{
    //other
    System.out.println("Arrrg, why not maytee?!");
    }
    //Ignore this test
    System.out.println("TEST" + AskPirate);


    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Use the equals() method to compare String objects, not the == operator.

    Please wrap all of the code in code tags, not just one part.

IMN logo majestic logo threadwatch logo seochat tools logo