Skip to main content

can't initialize a simple array

2 replies [Last post]
yotamoo
Offline
Joined: 2010-09-22

hey you all
i'm trying to initialize a databse, which is no more then a simple string array. i want to withdraw data from this array using another file.
here's my code:

class DataBase {

String[2] db;

void formatDataBase() {
for (i=0; i<=2; i++) {
db[i]=null;
}
}

// void addBrand(String brand) {
// for (int i=1; i<=2; i++) {
// if (db[i]==null) {db[i]=brand;}
// }
// }

}

but i get the following errors:

3 errors
yotam@main computer:~/Documents/java/Car$ javac DataBase.java
DataBase.java:3: ']' expected
String[2] db;
^
DataBase.java:3: ';' expected
String[2] db;
^
DataBase.java:3: expected
String[2] db;
^
3 errors
yotam@main computer:~/Documents/java/Car$

anyone? i'm lost, have no idea!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
darrylburke
Offline
Joined: 2010-08-05
wingetr
Offline
Joined: 2004-01-19

When declaring an array, you don't specify the size; you do that in the instantiation:

String[] db = new String[2];