java - Why is this code keep being "terminated"? -
java - Why is this code keep being "terminated"? -
i have code in eclipse:
package test; import java.util.scanner; class test{ public static void main(string args[]){ scanner input = new scanner(system.in); if (input.equals("payday2")){ system.out.println(input); } } }
now when seek start code/aplication, terminates itself.
any ideas why happens?
you instantiate scanner
variable named input
never seek read.
your condition
if (input.equals("payday2")){
will check if scanner object equals string "payday2" false, hence programme terminate.
if want read, need input.nextline()
.
i dont know eclipse, netbeans give warning "equals on incompatible type" line.
also, should not name variable capital letter convention, class name should start capital.
so fixed programme be
scanner input = new scanner(system.in); string value = input.nextline(); if ("payday2".equals(value)) { system.out.println(value); }
notice kept string in variable display displaying input
phone call tostring
of scanner
object not expected. notice compared string in reverse order practice avoid npe if not needed here. java
Comments
Post a Comment