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");



    }

    }
3
  • 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? Commented Mar 29, 2014 at 1: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.) Commented Mar 29, 2014 at 1: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 Commented Mar 29, 2014 at 13:27

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

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

etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.