user7890275
user7890275

Reputation:

Search for specific object

I have a list of objects, an example of an object is:

255, "Ben Nevis", "Highland", 1344.5, 56.796849, -5.003525

There are lots of objects in the list, and I want it to print out all the objects which would have the word "Ben" in them, but I am unsure of how to do this, and as my second field in object can have more than one name.

List<Mountains> list = Mountains.readMountains();
Scanner input = new Scanner(System.in);

System.out.println("Please enter a Mountain name: ");
String search = "Ben"
if (list.contains(search)){
    System.out.println("true");
}
else{
    System.out.println("no");
}

Upvotes: 1

Views: 216

Answers (5)

I.Miljancic
I.Miljancic

Reputation: 29

So if I'm correct you have a class that contains all the data that you have writen as an example and you store it that information in a list.

So you can use a for loop to go through the list and then compare the thing you looking for with the information you have inside the list.

 List<Mountains> list = Mountains.readMountains();
 Scanner input = new Scanner(System.in);

 System.out.println("Please enter a Mountain name: ");
 String search = "Ben"

 for(int i=0;i<list.size()-1;i++){

  if (list.get(i).mountainName.equals(search)){

    System.out.println("Using list.get(i) you print all the data that has 
                       the same name as your search");
    }

  }

Upvotes: 0

strash
strash

Reputation: 1321

list.stream().filter(m->m.getName().contains(search)).forEach(System.out::println);

and if you want something more fancy you can try this :)

the code above will print all matches containing that "search". If you want to check if it is found you can try this:

list.stream().filter(m->m.getName().contains(search)).findAny().isPresent()

These are nice 1-liners from Java 8 solving your question.

Upvotes: 1

deanmau5
deanmau5

Reputation: 871

String search = "Ben";
values.forEach(value-> {
    if (value.getName().contains(search)) {
        System.out.println("True");
    } else {
        System.out.println("False");
    }
});

Upvotes: 0

Caspar Noree
Caspar Noree

Reputation: 102

If I understood correctly you want to check for a string with a specific char sequence in a list of objects?

I would do something like this:

    String keyword = "Ben";
    Object[] objects = new Object[] { 255, "Ben Nevis", "Highland", 1344.5, 56.796849, -5.003525};

    for(Object obj : objects)
        if(obj instanceof String)
            if(((String) obj).contains(keyword)) {// Response here}

Upvotes: 0

f1sh
f1sh

Reputation: 11934

You have to iterate over every entry in your list, then check if the Name property (which is a String) contains your search term:

for(Mountains m:list){
  if(m.getName().contains(search)){
    System.out.println(m);
  }
}

This assumes that a Mountain's name can be accessed using getName().

In your current code, you check (using contains) if a list of Mountains objects contains String object with the value of "Ben" which is never gonna be true.

Upvotes: 1

Related Questions