Home > OS >  Java List/ArrayList of Strings
Java List/ArrayList of Strings

Time:06-12

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

package me.chiz.cava.AutoMute;

import java.util.ArrayList;

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

CodePudding user response:

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.

  • Related