4

I have created arrays for strings and integers I want to use in my program and I want to use them instead of using

(name.equals "barry"||"matty"

for example.

I want to know how to write the if statement to check the user input against the strings in the array.

import java.util.Scanner;

public class Username
{
    public static void main (String[]args)
    {
        Scanner kb = new Scanner (System.in);
        // array containing usernames 
        String [] name = {"barry", "matty", "olly","joey"}; 

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

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

        int [] floor = {0,1,2,3,4,5,6,7};

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

5 Answers 5

3

I think you're just looking for List.contains - but that requires a List rather than an array, of course. There are two obvious options here.

Firstly, you could use a List<String> to start with:

List<String> names = new ArrayList<>();
names.add("barry");
names.add("matty");
names.add("olly");
names.add("joey");
...
if (names.contains(name))
{
    ...
}

Alternatively, you could use Arrays.asList to create a view:

String[] names = {"barry", "matty", "olly", "joey"}; 
List<String> namesList = Arrays.asList(names);
...
if (namesList.contains(name))
{
}

As a third option, if you put make your names array sorted (either by hand or by calling Arrays.sort) you could use Arrays.binarySearch to try to find the name entered by the user:

String[] names = {"barry", "matty", "olly", "joey"}; 
Arrays.sort(names);
...
if (Arrays.binarySearch(names, name) >= 0)
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

Arrays are very low-level structures that don't provide any method. You'd better use collections instead, which have a contains() method:

Set<String> names = new HashSet<>(Arrays.asList(new String[] {"barry", "matty", "olly","joey"}));

if (names.contains(name)) {
    ...
}

Since you don't seem to care about the order of the names, but only want to test if the collection contains a name or not, a HashSet is the best data structure: HashSet.contains() runs in constant time (O(1)), whereas List.contains(), for example, is O(n).

Read the collections tutorial.

Comments

2

You can loop through your array instead:

String name = kb.nextLine();
if(contains(name)) {
    System.out.println("you are verified you may use the lift");
}


public boolean contains(String name) {
    String [] names = {"barry", "matty", "olly","joey"}; 
    for (int i = 0; i < names.length; i++) {
        if(names[i].equals(name)) {
            System.out.println("you are verified you may use the lift");
        }
    }

Or you can use List.contains(), in this case you have to add your names inside List instead of regular array.

For Example:

String[] names = {"barry", "matty", "olly", "joey"}; 
List<String> namesList= Arrays.asList(names);

if (namesList.contains(name)) {
    System.out.println("you are verified you may use the lift");
}

Comments

1

Using ArrayList and List instead of Array of Strings:

List<String>names = new ArrayList<String>(names); 
name = kb.nextLine();
if(names.indexOf(name)>-1)System.out.println("you are verified you may use the lift");

This because of the indexof in List returns -1 if not found or the index of the founded element, starting with 0.

I think that also

if(names.contains(name))System.out.println("you are verified you may use the lift");

works

Comments

0

Use a for loop

get input
for int i = 0, i < array size i++
if input.equals(array[i]) then do stuff

If you switch to an arraylist, you can do if(array.contains(input))

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.