-3

I'm building a Java program where the database of it is going to be a class named Words, with Strings Vectors that will store the infos I need.

So I just created that class and also created the Vector in that same class, named English. But when I try to add names to that String Vector, it gives me an error message, saying that I need to "Create a class named English in my DataBase package, or create a class inside this own class.

Why can't I simply just put those data on a variable on the class I created? How should I accomplish this?

Btw, here is my code:

package DataBase;


public class Words {

public  String English[];

English[0] = "Doll";
English[1] = "Machine";


}

3 Answers3

1

The assignment

English[0] = "Doll";
English[1] = "Machine";

should be done inside a block/method i.e. Words constructor

public Words() {
    English = new String[2];
    English[0] = "Doll";
    English[1] = "Machine";
}
Ale Zalazar
  • 1,875
  • 1
  • 11
  • 14
  • It is advised to use lower camel case for naming variables i.e. "english" instead of "English" – Ale Zalazar Dec 10 '13 at 15:50
  • Thanks! But why should this be done that way? And how am I gonna instantiate those words on my main method? Like this? Words word = new Words(); System.out.println(word.English[0]); – user3063514 Dec 10 '13 at 15:52
  • If you want to add the words from outside, you need to add a method public void addWord(String word) { //do something}. It will be easier if you use a more dynamic structure like a java.util.List instead of an Array. Actually, from outside, you could add words with words.English[n] = "new word";, because English has public visibility, but this is not recommended. It is better to use private visibility. – Ale Zalazar Dec 10 '13 at 15:54
0

You need to allocate memory for the array in the beginning.

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html. Please note the definition of the array:

"An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created"

Update: I had just a very quick look at your code; indeed you need to execute it in some block. Have a look at some nice Java tutorials (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html),

acostache
  • 2,177
  • 7
  • 23
  • 48
0

Try to read about java syntax: Arrays

You've only create an array definition (I've change name a little bit to match standards):

public class Words {
     private  String[] english;
}

Now you need to instantiate it:

public class Words {
     private  String[] english = new String[2];
}

or

public class Words {
     private  String[] english;

     public Words() {
         english = new String[2];
     }
}

After that it's time to populate array:

public class Words {
    private String[] english = new String[]{"Doll","Machine"};
}

or

public class Words {
     private  String[] english;

     public Words() {
         english = new String[2];
         english[0] = "Doll";
         english[1] = "Machine";
     }
}

Note that in example we used arrays not Vectors like you've mention above. One more thing, I've used and private modifier, so you need to create getter and setter methods for you're array.

zawi
  • 58
  • 9
  • Thanks! That really helped me. Now one last question: is it really mandatory to put the english array as private? I don't get why people put it private to make all those gets and sets while they could just leave it public. – user3063514 Dec 10 '13 at 16:42
  • I suggest you read ["Why use getters and setters?"](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters). – zawi Dec 11 '13 at 10:08