java - Boolean and if mistake -
this question has answer here:
- how compare strings in java? 23 answers
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
Post a Comment