-5

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));
        }

    }
    } 



}
azro
  • 53,056
  • 7
  • 34
  • 70

2 Answers2

0

Change

public void run ()

To

public void init()

Or

public static void main(String[] args)

run() is a method of Java's in-built interface called Runnable and it's used in Threading, and it doesn't look like you're using any Threading here.

Russ J
  • 828
  • 5
  • 12
  • 24
  • More info on the differences between run() and init() https://stackoverflow.com/questions/261428/entry-point-for-java-applications-main-init-or-run – Russ J Feb 21 '19 at 21:25
  • Thank you so much, I'm sorry for making this question, I'm a total beginner – Atila Sabo Feb 23 '19 at 21:14
  • Don’t apologize, we were ALL beginners at some point. I will admit that Stack Overflow can be a little intimidating for newbies, but don’t let that detract you. I promise everyone here wants to see you become better. – Russ J Feb 23 '19 at 23:45
0

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));
        }

    }
}
Russ J
  • 828
  • 5
  • 12
  • 24
  • Oh :( I can't believe I was so careless. Thank you so much. I'm sorry I posted so stupid question, I'm a total beginner. – Atila Sabo Feb 23 '19 at 21:16