java - Boolean and if mistake -


this question has answer here:

i have problem code. process -

  • first enter name
  • then enter age
  • then enter password (default value "anybody")

name , age can entered without problem program evaluates password error. when enter password correctly, still returns false.

please me!! thanks.

    import java.util.scanner;      public class myclass {      public static void main(string[] args) {         string name;         int age;         boolean trueorfalse;         boolean trueorfalse2;          string builtinpassword = "anybody";          scanner keyboardinput = new scanner(system.in);              system.out.print("please enter first name: ");             name = keyboardinput.next();             system.out.print("please enter age: ");             age = keyboardinput.nextint();             trueorfalse = false;             trueorfalse2 = true;             system.out.print("please enter password: ");         if (keyboardinput.next() == builtinpassword) {             system.out.println(trueorfalse2);         } else {             system.out.println(trueorfalse);         }       } } 

you need use .equals() on string this:

public static void main(string[] args) {     string name;     int age;     boolean trueorfalse;     boolean trueorfalse2;      string builtinpassword = "anybody";      scanner keyboardinput = new scanner(system.in);          system.out.print("please enter first name: ");         name = keyboardinput.next();         system.out.print("please enter age: ");         age = keyboardinput.nextint();         trueorfalse = false;         trueorfalse2 = true;         system.out.print("please enter password: ");     if (keyboardinput.next().equals(builtinpassword)) {         system.out.println(trueorfalse2);     } else {         system.out.println(trueorfalse);     }  } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -