Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginning Java Interface Program Collecting Strings and Printing
    primarykey
    data
    text
    <p>I am trying to write a program that reads all words from user inputted scanner, puts them into an ArrayList calls a method, and prints a list of all the words that have a length of less than five. Yes, I can write this in a single program but the purpose of the assignment is to work with interfaces and inheritance. I wrote a program that loops user input into an array of objects. I wrote an interface (this cannot be changed), and i implemented a class which scans the words from the array and gives a boolean value depending on whether the word is less than five or more than five letters. I wrote a method that takes the answer from the class and creates a new ArrayList of objects. If the word is less than five letters then it is added to the array. I am trying to call the method into my main but i am getting a "Filters is abstract" " Cannot be instantiated" error...but my interface is not abstract? I have no idea how to fix it and its driving me crazy....Any help is much appreciated. Thank you!</p> <pre><code>public interface Filters { boolean accept(Object x); //this interface cannot be changed. } public class SWordFilter implements Filters { //This is my subclass for the interface public boolean accept(Object x) { String y =(String) x; boolean accept=false; if (y.length()&lt; 5) accept=true; return accept; } } import java.util.ArrayList; import java.util.Scanner; public class MyHomework { //this is my main and my method. I cannot call the method. public static void main(String[] args) { ArrayList&lt;Object&gt; names=new ArrayList&lt;Object&gt;(); Scanner in=new Scanner(System.in); int i=0; while(i&lt;5) { System.out.println("Enter the words"); names.add(in.next()); i++; } Filters tran= new Filters(names); Object result=collectAll(names,tran); } public static ArrayList&lt;Object&gt; collectAll (ArrayList&lt;Object&gt; list, Filters f) { ArrayList&lt;Object&gt; result= new ArrayList&lt;Object&gt;(); for (int x=0; x&lt;5; x++) { if (f.accept(list.get(x))) { result.add(list.get(x)); } else { System.out.print("the word is too long"); } //SWordFilter julie= new SWordFilter(); //System.out.print(julie.accept(names.get(j))); } return result; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload