0

I am trying to tell whether that string was found in the list or not. For instance, if I put Max in my list and search for Max, it should say "Max was found" If not, then it should say "Max was not found"

I do not know how to approach to getting the answer from here.

import java.util.ArrayList;
import java.util.Scanner;

public class OnTheList {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        ArrayList<String> list = new ArrayList<>();
        while (true) {
            String input = scanner.nextLine();
            if (input.equals("")) {
                break;
            }

            list.add(input);

        }

        System.out.print("Search for? ");
        System.out.print(scanner.nextLine());


        if (list.contains(list)) ----> I think this is the part where I am not getting it
            System.out.println(" was found!");
        else 
            System.out.println(" was not found");

    }
}
2
  • 4
    Hint 1: list.contains(list) is checking whether the list contains itself. Hint 2: your final scanner.nextLine() call is just used to print something, but you're not "remembering" that in a variable anywhere. How are you meant to know what you're checking for? Commented Jun 17, 2020 at 7:06
  • I somehow thought I had to use input instead of list but I was wrong. Thanks for the help Commented Jun 17, 2020 at 7:14

2 Answers 2

1

You may store the word to search, here you ask for it with scanner.nextLine() and print it but didn't save it. Then use the variable you saved the word in, to check into the List

System.out.print("Search for? ");
String toSearch = scanner.nextLine();

if (list.contains(toSearch)) 
    System.out.println(toSearch + " was found!");
else 
    System.out.println(toSearch + " was not found");
Sign up to request clarification or add additional context in comments.

1 Comment

Got it. As mentioned above, I have to save it.
0

Here you didn't store the user input that you are getting from Search for ,and you are trying to search an element of the list but passing list as the argument for the contains() method ,So first store the user input for a variable the search that input by passing it as the argument to contains() method and make sure to close the scanner variable at the end of the program to avoid memory leaks like below.

import java.util.ArrayList;
import java.util.Scanner;

public class OnTheList {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    ArrayList<String> list = new ArrayList<>();
    while (true) {
        String input = scanner.nextLine();
        if (input.equals("")) {
            break;
        }

        list.add(input);

    }

    System.out.print("Search for? ");
    String toSearch = scanner.nextLine(); 


    if (list.contains(toSearch))  
        System.out.println(" was found!");
    else 
        System.out.println(" was not found");

    scanner.close();

}
}

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.