Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java List/ArrayList of Strings

So I’m making a Spigot Minecraft Plugin and its to auto mute players.
I’m making an List/ArrayList of Strings which are censored.
But it doesn’t let me add to it.
Am I doing something wrong?
Here is my code:

ArrayList<String> censored = new ArrayList<String>();
censored.add("example1");

Thank you!

//edit

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package me.chiz.cava.AutoMute;

import java.util.ArrayList;

public class Words {
    ArrayList<String> censored = new ArrayList<String>();
    censored.add("example1");
}

>Solution :

You can’t just dump a bunch of code in a class file. A class file defines behaviours – it isn’t, itself, ‘a thing you can run’. censored.add("example1") is a statement, it has to be in a method or constructor or initializer block.

Wrong:

class Example {
  ArrayList<String> censored = new ArrayList<String>();
  censored.add("example1");
}

correct:

class Example {
  ArrayList<String> censored = new ArrayList<String>();

  // This is a constructor
  Example() {
    censored.add("example1");
  }
}

This is extremely basic java. If this is throwing you for a loop sufficient to start asking questions on SO, you’re trying to run before you can walk. Find a java tutorial and go through it carefully. Forget about hacking on minecraft for now, learn some java first. Then get back to it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading