+ 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
5 Answers
+ 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
}
}
+ 4
Yes, I agree with you Shafay.
+ 1
i would suggest you use file I/O for that, makes it easier to compare.
+ 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");
}
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!