-2

If someone can help me with this. I'm complete -n o o b, only started to learn and got stuck.

If I'm asking this -

Scanner buck = new Scanner(System.in);
String fname;
System.out.println("Please Enter your Name ");
fname = buck.next();

which command do I use to make specific name only to be entered as an answer. For example name would be Vani.

If name is "Vani" than "you are in". If any else name "than you go out".

I understand this with numbers but not with letters. Any help would be appreciated.

Vadim
  • 1
  • 2

2 Answers2

1

To "kick out" if the name is not "Vani":

if("Vani".equals(fname)) { //You can use equalsIgnoreCase instead if you like
    System.out.println("You are in.");
} else {
    System.out.println("You are out.");
}

To accept input until "Vani" is given:

do {
    System.out.println("Please Enter your Name ");
    fname = buck.next();
    if(!"Vani".equals(fname)) {
        System.out.println("You're not Vani!");
    }
} while(!"Vani".equals(fname));
Zircon
  • 4,677
  • 15
  • 32
  • Please make an "Edit" section in your original question and format this code. Also please specify the exception you're seeing. I'm not sure if you can do this because your question was marked as a duplicate, though. – Zircon May 06 '16 at 14:15
0
if ("Vani".equals(fName)) {
    // you go in
} else {
    // cannot go in
}

And if you want case insensitive check, do "Vani".equalsIgnoreCase(fName).

Tom
  • 16,842
  • 17
  • 45
  • 54
Vishal
  • 549
  • 3
  • 13