Simple login with Java code - using conditional assignment -
this question has answer here:
- how compare strings in java? 23 answers
i'm still newbie java programming. , can tell me what's wrong source code? when run code, conditional assignment outputs "login failed".
import java.util.scanner; public class programbiodatamahasiswa { public static void main(string[] args) { scanner input = new scanner(system.in); string username, password, output; system.out.print("enter username : "); username = input.nextline(); system.out.print("enter password : "); password = input.nextline(); output = (username=="kesit" && password=="ps123") ? "login successfully" : "login failed" ; system.out.println(output); } }
with strings ("quest" , "ps123") shouldn't using == check if equal. compare pointer , due fact string in java immutable, pointers different. therefore use
username.equals("kesit") && password.equals("ps123").
that should work!
Comments
Post a Comment