+ 2

How to check if user input equal to a string?

How do i check it?

5th Aug 2016, 6:14 PM
Aady Ahmed
Aady Ahmed - avatar
6 ответов
+ 4
String s = "test"; Scanner input = new Scanner(System.in); if(s.equals(input.nextLine()){ Your code here }
5th Aug 2016, 7:27 PM
James
James - avatar
+ 2
Option 1:Main method always takes Strings in fact string array. #2:For any other interface like we apps. You can cast to String variable in try catch #3: or instance of method can beused
5th Aug 2016, 7:46 PM
Praveen
+ 1
you use if, else, and else if statements.
5th Aug 2016, 6:32 PM
Aquarius
Aquarius - avatar
+ 1
by using equals method rather than == operator equal to operator checks there address not the value bt equal method compares content of string
5th Aug 2016, 7:23 PM
sachin tomar
sachin tomar - avatar
+ 1
Thanks for the help got it working!
5th Aug 2016, 8:56 PM
Aady Ahmed
Aady Ahmed - avatar
0
use scanner method to get input from user. Let it be String n. And then use if (n.equals (required string)==0){} else {}
6th Aug 2016, 7:38 AM
Jasbinder Singh
Jasbinder Singh - avatar