0

I am creating a simple lift programme in java. I want to to have four users that are allowed to use the lift i have already got it working for 1 but i cant figure out how to check multiple strings using the one if statement.

import java.util.Scanner;

public class Username

{
public static void main (String[]args)
{

    Scanner kb = new Scanner (System.in);
    String name; 


    System.out.println("Enter your name");
    name = kb.nextLine();
    if (name.equals("barry "))
        System.out.println("you are verified you may use the lift");


    Scanner f = new Scanner(System.in);
    int floor;


    System.out.println("What floor do you want to go to ");
    floor = f.nextInt();

    if (floor >7)
        System.out.println("Invalid entry");
    else if (floor <= 7)
        System.out.println("Entry valid");



    }

    }
user3151959
  • 91
  • 2
  • 10
  • 1
    Hopefully this is homework and not the beginnings of a real elevator control system. Do you know how to check for the presence of the supplied string in an array of strings? – Jason Aller Mar 29 '14 at 01:39
  • Side note: You don't need to say `else if (floor<=7)`; just `else` is good enough, because if we get there we already know that `floor<=7` must be true. (Adding a redundant condition like this _could_ lead to "might not have been initialized" errors in some cases.) – ajb Mar 29 '14 at 01:48
  • no im trying to figure it out now, ive now got the strings in an array and i am trying to figure out what to write for the user input statement – user3151959 Mar 29 '14 at 13:27

3 Answers3

1

Check out this related question:

Test if a string contains any of the strings from an array

Basically, put the names into an Array of strings, and compare the name entered with each name in the Array.

Community
  • 1
  • 1
Ben Harris
  • 1,734
  • 3
  • 15
  • 24
0

Use the OR symbol "||" or "|".

Such as if (name.equals("barry ") || name.equals("sara"))

For future reference the difference between the two is "||" short circuits. In this situtation, if barry is the name then the second statement for checking against sara will never be executed.

Mason T.
  • 1,567
  • 1
  • 14
  • 33
0

basically, you need an "Or" gate, this would work:

if(name.equals("name1")||name.equals("name2")||name.equals("name3"))

etc...

Bary12
  • 11
  • 1
  • 3