+ 2

How to create a loop if input string is equal to defined string?

i mean... i want to create a program in which user have to put his name. if his name was in list then.... output has to be... welcome... in java

27th Aug 2017, 9:57 AM
Manjit Kumar
Manjit Kumar - avatar
5 Respuestas
+ 6
Here's an example. I hope it will help you :) import java.util.Scanner; public class UserCheck { static String[] listOfUsers=new String[] {"Leo","Tea","George"}; // define list of users names public static void main(String[] args) { System.out.print("Please, input your name: "); Scanner inputName = new Scanner(System.in); String newUser=inputName.next(); // if user entered first and last name, more then one word, then use: String newUser=inputName.nextLine() // this is loop : for(String s: listOfUsers) { if (newUser.equals(s)) { System.out.println("Welcome dear "+newUser+" in Java World"); } } inputName.close(); // to stop the resource leak } }
27th Aug 2017, 11:12 AM
LukArToDo
LukArToDo - avatar
+ 4
Yes, I agree with you Shafay.
27th Aug 2017, 11:26 AM
LukArToDo
LukArToDo - avatar
+ 1
i would suggest you use file I/O for that, makes it easier to compare.
27th Aug 2017, 11:04 AM
Shafay Haseeb
Shafay Haseeb - avatar
+ 1
can't it get simpler like importing scanner then declaring variables. string a; then if(a==man){ system.out.println("welcome"); }else{ system.out.println("get out"); }
27th Aug 2017, 6:07 PM
Manjit Kumar
Manjit Kumar - avatar
0
Lukar,thats great but i think u should add "\n" after please enter name. secondly an else block would make this code more user friendly. Great effort!
27th Aug 2017, 11:20 AM
Shafay Haseeb
Shafay Haseeb - avatar