0

Who can tell me which mistake i made?

This is the codes import java.util.Scanner; public class Program { public static void main(String[ ] args) { String input1 = new Scanner(System.in).nextLine(); String input2 = new Scanner(System.in).nextLine(); if (input1 == piaoyi){ System.out.println("smart");} else if (input2 == xujun){ System.out.println("stupid");} } }

24th Sep 2018, 4:37 AM
郎ć»ș束
郎ć»ș束 - avatar
6 Answers
+ 10
/*you only need to create one object of Class Scanner*/ Scanner sc = new Scanner(System.in); String str1 = sc.nextLine(); String str2 = sc.nextLine(); if(str1.equals(str2)){/*doSomthing*/} else{/*doSomthingElseIfNotEqual*/}; System.out.print(str1+"\n"+str2);
24th Sep 2018, 7:36 AM
D_Stark
D_Stark - avatar
+ 2
piaoyi and xujun look like variable names, but they haven't been defined. Did you mean to write them as strings for comparison? wrap them in " " and the code will compile. Also in Java you should compare strings with .equals() rather than == e.g. "myString".equals("anotherString")
24th Sep 2018, 5:37 AM
Dan Walker
Dan Walker - avatar
+ 1
Like Dan Walker said you should use the method equals() to test for the actual value with objects, because when you use == you're only testing if both objects have the same reference.
24th Sep 2018, 6:36 AM
Chriptus13
Chriptus13 - avatar
0
Plz,come one to help me
24th Sep 2018, 4:37 AM
郎ć»ș束
郎ć»ș束 - avatar
0
Maybe try this: if (input1 == "piaoyi"){...} and else if (input2 == "xujun"){...} If it doesn't work, try this: if (input1.equals("piaoyi")){...} and if (input2.equals("xujun")){...}
24th Sep 2018, 5:40 AM
Jan Ơtěch
Jan Ơtěch - avatar
0
Thanks,guys.
24th Sep 2018, 7:20 AM
郎ć»ș束
郎ć»ș束 - avatar