user3550216
user3550216

Reputation: 19

Hash Set giving error when comparing an input

Trying to check a password against values in a hash set. The error appears to be in the myset.contains but cant see why.

Set<String> myset = new HashSet<>();
myset.add("Apples");
myset.add("Bananas");

String inputPass;
Scanner input = new Scanner(System.in);

System.out.println("Fruit?: ");
inputPass = input.nextLine();

if (inputPass.equals(myset.contains)) {
    // Lecturer.printMe();
    System.out.println("Welcome");
}
else {
    System.out.println("ACCESS DENIED");
}

Would appreciate some guidance.

Upvotes: 0

Views: 38

Answers (2)

Kaiser Keister
Kaiser Keister

Reputation: 81

"contains" is a method, not a field. Also, it accepts an instance of T (in this case, a String) as a parameter.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522506

You use of Set#contains does not look right to me. Try using this version:

if (myset.contains(inputPass)) {
    System.out.println("Welcome");
}
else {
    System.out.println("ACCESS DENIED");
}

Upvotes: 0

Related Questions