2

I need to write a code that checks all the words in an array list and tells me how many of these have a certain length. I understand how to do that, but I can't figure out how to make the ArrayList to be read in the second class so I can apply it in the program.

MAIN

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    java.util.ArrayList myList = new java.util.ArrayList();
    myList.add("cat");
    myList.add("mouse");
    myList.add("frog");
    myList.add("dog");
    myList.add("dog");
    int len = in.nextInt();
    WordList wl = new WordList();
    wl.numWordsOfLength(len);
  }
}

SECOND CLASS

public class WordList{
  public int numWordsOfLength(int len){
    int count = 0;
    for(int i=0;i<len;i++){

      if(((String)myList.get(i)).length()==len){
        count++;
      }
    }
    return count;
  }
}
0

3 Answers 3

2

You need to pass the list as parameter:

  public int numWordsOfLength(int len, List<String> myList){
    int count = 0;
    for(int i=0;i<myList.size;i++){

      if((myList.get(i)).length()==len){
        count++;
      }
    }
    return count;
  }

or via the WordList Constructor:

import java.util.List;
public class WordList{

    private final List<String> myList;
    WordList(List<String> myList){
        this.myList = myList;
    }
    public int numWordsOfLength(int len){
        int count = 0;
        for (String s : myList) {
            if (s.length() == len) {
                count++;
            }
        }
        return count;
    }
}

Side node instead of :

    java.util.ArrayList myList = new java.util.ArrayList();

just do :

   import java.util.List;
   ....
   List<String> myList = new ArrayList();

Full Running example:

public class Test {

    public static class WordList{

        private final List<String> myList;
        WordList(List<String> myList){
            this.myList = myList;
        }
        public int numWordsOfLength(int len){
            int count = 0;
            for (String s : myList) {
                if (s.length() == len) {
                    count++;
                }
            }
            return count;
        }
    }


        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            List<String> myList = new ArrayList<>();
            myList.add("cat");
            myList.add("mouse");
            myList.add("frog");
            myList.add("dog");
            myList.add("dog");
            int len = in.nextInt();
            WordList wl = new WordList(myList);
            System.out.println(wl.numWordsOfLength(len));
        }
}
Sign up to request clarification or add additional context in comments.

Comments

2

This depends on your interpretation of "read in the second class".

My guess is you want the second class to have "myList" as a private variable, so the code will work when you call the numWordsOfLength() function.

In order to do this, you should pass the array you built to WordList as a dependency. There are generally two ways to give an object a dependency. Either when you create the object (constructor based) or later by calling a method (setters).

So in main, your goal would be to do either:

WordList wl = new WordList();
wl.setList(myList)
wl.numWordsOfLength(len);

Or:

WordList wl = new WordList(myList);
wl.numWordsOfLength(len);

Either way, you will have to add "myList" variable inside the WordList and then expose it either via a separate setter method (setList) or in the constructor itself.

I'd prefer a constructor in this case, since the name of the class "WordList" implies that it IS a list of things and therefore should encapsulate the thing it is.

i.e.

public class WordList{

  private List<String> myList;

  public WordList(List<String> listOfWords) {
    this.myList = listOfWords;
  }
  
  public int numWordsOfLength....
}

Comments

1

You don't need that second class at all, if you use Java 8+:

    ...
    java.util.ArrayList<String> myList = new java.util.ArrayList();
    myList.add("cat");
    myList.add("mouse");
    myList.add("frog");
    myList.add("dog");
    myList.add("dog");
    int len = in.nextInt();
    int count = myList.stream().map(String::length).filter(c->c==len).count(); <--that's your answer
    ...

If you prefer to use that second class, you can just use this "count" expression in your numWordsOfLength method in the WordList class once you've passed the list there as the previous answers suggest.

BTW, the loop in your WordList class seem to be incorrect: you're only iterating the list up to len, instead you want to iterate over the entire list. I assume the len variable is the searched lengths of a word:

 public int numWordsOfLength(int len, List<String> list){
    int count = 0;
    for(int i=0;i<list.size();i++){
      if(list.get(i).length()==len){
        count++;
      }
    }
    return count;
  }

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.