Eiko Leung
Eiko Leung

Reputation: 13

Search ArrayList for keywords and return location

I am trying to write a method that search an ArrayList of a particular word and then prints the location of all of the occurrences of the word.

Here is what I have, it works fine until I enter the word I want to search but then it prints nothing:

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

public class W7E2 {
    public static void main(String[]args) {
        System.out.println("Please anter words: ");
        Scanner sc = new Scanner(System.in);
        String []w = sc.nextLine().split(" ");

        ArrayList<Words> word = new ArrayList<Words>();
        for(int i=0; i<w.length; i++) {
            word.add(new Words(w[i]));
        }
        System.out.println(word);

        System.out.println("Please enter the word you want to search: ");
        String search = sc.nextLine();


        for(Words ws: word) {
            if(ws.equals(search)) {
                System.out.println(ws.getLocation());
            }
        }

    }

    static class Words{
        private String wor;
        private static int number = -1;

        public Words(String wor) {
            this.wor = wor;
            number++;
        }
        public int getLocation() {
            return number;
        }

        public String toString() {
            return wor;
        }
    }
}

Upvotes: 0

Views: 75

Answers (4)

SVG
SVG

Reputation: 924

What you should do is instead of ws.equals(search) you need to add ws.toString().equals(search) as you return the word from the toString()
method in the Words Class. So the code should look something like this,

  for(Words ws: word) {
            if(ws.toString().equals(search)) {
                System.out.println(ws.getLocation());
            }
        }

Upvotes: 0

jay shah
jay shah

Reputation: 300

Your code should be like this :

for(Words ws: word) {
    if(ws.toString().equals(search)) { //to change
        System.out.println(ws.getLocation());
    }
}

ws is the object of Words class, you have to change it to toString()

Upvotes: 0

flyingfox
flyingfox

Reputation: 13506

Besides GBlodgett's answer,the number in class Word is static,so each Word instance will have a same number,you need to use a no-static variable to store the location

static class Words{
    private String wor;
    private static int number = -1;
    private int location;

    public Words(String wor) {
        this.wor = wor;
        number++;
        location = number;
    }
    public int getLocation() {
        return location;
    }

    public String toString() {
     return wor;
   }
}

Upvotes: 0

GBlodgett
GBlodgett

Reputation: 12819

In your if statement to see if the ArrayList contains the word you have:

if(ws.equals(search)) {
    System.out.println(ws.getLocation());
}

But ws is a Word object and unless you override the equals() method, it will never equal the String object. You need to do something like:

if(ws.getwor().equals(search)) {
        System.out.println(ws.getLocation());
}

This is assuming that you create a get method for wor.

Upvotes: 2

Related Questions