user1735876
user1735876

Reputation:

Error on defining arrays in class

I am trying to make a class that will read a text file of names into an array, then return that array to the main class. However I am having an error when attempting to define the arrays.

public class Test{
String[] foo;
String[] zoo;
String[] yoo;
}

I get an error on String[] yoo

Syntax error on token ";", { expected after this 
token

I really have no clue what is going on, can anyone help?

Edit - Actual section of code

    String[] swords;
    String[] prefix;
    String[] suffix;
    String[] rarity;
    String[] colors = {"2","3","4","5","6","7","9","a","b","c","d","e","f"};
    String[] bows = new String[3];
    String[] enchantments = {"Frost","Igniton","Projection","Explosion","Enhance Jump","Enhance Speed","Resist Flames","Invisibility"};
    rarity = new String[1000];
    swords = new String[1000];
    bows = new String[1000];
    prefix = new String[1000];
    suffix = new String[1000];

Upvotes: 1

Views: 100

Answers (4)

pinkpanther
pinkpanther

Reputation: 4808

You should not initialize like this in outside the constructors or methods

Wrong:

public Test{
 String[] rarity;
 String[] swords;
 rarity = new String[1000]; 
 swords = new String[1000];
}

You can do this

public Test{
      String[] rarity = new String[1000]; 
      String[] swords = new String[1000];
    }

if the variables are static you can use static block

public Test{
   private static int x;
   static{
          x=2;
   }

}

Use constructor instead to initialize:

 public Test{
    String[] swords;
    String[] prefix;
    String[] suffix;
    String[] rarity;
    String[] colors = {"2","3","4","5","6","7","9","a","b","c","d","e","f"};
    String[] bows = new String[3];
    String[] enchantments = {"Frost","Igniton","Projection","Explosion","Enhance Jump","Enhance Speed","Resist Flames","Invisibility"};
  public Test(){
    rarity = new String[1000];
    swords = new String[1000];
    bows = new String[1000];
    prefix = new String[1000];
    suffix = new String[1000];
  }
}

That's all

Upvotes: 1

user1779715
user1779715

Reputation:

First of all, you should make them public or private(unless you really need it to be package-private).

An array is created like this: Type[] variableName = new Type[length];

length is the size of the array, for example String[] test = new String[5] can contain 5 strings. To set them use test[i] = someString; where i is the index(starting at 0 and ending at length - 1).

You can also make an ArrayList if you do not want your array to be limited, but that uses a bit more memory.

ArrayList<Type> variableName = new ArrayList<>();

For example: ArrayList<String> test = new ArrayList<>();

To add to it use test.add(someString) and to get: arrayList.get(i) where i is the index.

A disadvantage of ArrayList is that primitive types(int, byte, boolean, ...) cannot be used. You'll need to use Integer, Byte, Boolean, ...

If you have an ArrayList<Integer>, you could intArrayList.add(5) because autoboxing transforms 5 into new Integer(5).

Upvotes: 0

Vegard
Vegard

Reputation: 1942

unless you post all of your code it is not possible to be sure the answer is correct.

but I guess you have this:

rarity = new String[1000];
swords = new String[1000];
bows = new String[1000];
prefix = new String[1000];
suffix = new String[1000];

outside a method. that is not possible in Java.

do like this instead:

String[] rarity = new String[1000];

or init the field inside a method/constructor

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692231

You can't assign values to fields outside of the field declaration or a block (or constructor). So this line

rarity = new String[1000];

(and the other similar ones) should be in the constructor, or the declaration should also initialize the field:

String[] rarity = new String[1000];

Upvotes: 2

Related Questions