Testing string and integer arrays with user input

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

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

eg.

I want to know how to write an if statement to check user input for strings in an 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");
    }
}
+4
source share
5 answers

I think you are just looking List.contains- but this requires, rather, Listnot an array. There are two obvious options here.

First, you can use List<String>to start:

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

Alternatively, you can use Arrays.asListto create a view:

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

, , ( , Arrays.sort), Arrays.binarySearch , :

String[] names = {"barry", "matty", "olly", "joey"}; 
Arrays.sort(names);
...
if (Arrays.binarySearch(names, name) >= 0)
{
    ...
}
+3

:

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

List.contains(), List .

:

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

, . , contains():

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

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

, , , , , HashSet - : HashSet.contains() (O ( 1)), List.contains(), , O (n).

.

+2
source

Using ArrayList and List instead of an 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 is due to the index in the List, returning -1 if not found, or the index of the based item, starting at 0.

I think and

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

work

+1
source

Use the for loop

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

If you switch to arraylist, you can doif(array.contains(input))

0
source

Source: https://habr.com/ru/post/1534042/


All Articles