Array List Java [closed]

Please can somebody help me? I’m learning about Array List in Java, I found interesting things in this lesson (Stanford CS106a)

https://www.youtube.com/watch?v=YJ9FlCFi3c8&feature=youtu.be&list=PL84A56BC7F4A1F852&t=1385

But when I tried to run this program in Eclipse it gives me many errors 🙁

What’s wrong with this? (Why is void invalid type for the variable printList?)

import java.util.ArrayList;
import acm.program.*;
class ArrayListEx extends ConsoleProgram {

    public void run () {

        ArrayList<String> sList = new ArrayList <String>();

        readList (sList); 
        printList (sList);  

        readList (sList);  
        printList (sList);


    private void readList (ArrayList list)  {


        while (true) {

            String line = readLine("Unesi tekst");
            if (line.equals("")) break;
            list.add(line);  
        }

    }

    private void printList (ArrayList lista) {

        println ("List contains: " + lista.size() + " elements.");

        for (int i = 0; i< lista.size(); i++) {

            println(lista.get(i));
        }

    }
    } 



}

Answer

The issue was no } bracket on your run function and an extra } at the end of your printList function. Also have changed run() to init(). This should hopefully work for you.

import java.util.ArrayList;
import acm.program.*;


public class ArrayListEx extends ConsoleProgram {


    public void init () {
       ArrayList<String> sList = new ArrayList <String>();

       readList (sList); 
       printList (sList);  

       readList (sList);  
       printList (sList);
   }


   private void readList (ArrayList list)  {
        while (true) {
            String line = readLine("Unesi tekst");
            if (line.equals("")) break;
            list.add(line);  
        }

    }

    private void printList (ArrayList lista) {

       println ("List contains: " + lista.size() + " elements.");

        for (int i = 0; i< lista.size(); i++) {

            println(lista.get(i));
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *