Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Can't initialize a simple array

    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:

    PHP Code:
    class DataBase {

    String[2db;

    void formatDataBase() {
    for (
    i=0i<=2i++) {
    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: <identifier> expected
    String[2] db;
    ^
    3 errors
    yotam@main computer:~/Documents/java/Car$

    anyone? i'm lost, have no idea!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You cannot specify the size of the array in the datatype. You need to do that in the initialization: String[] db = new String[2];.
    Your 'i' also needs a datatype, probably an int. You should also get the length of the db variable if you want to loop it; the above will throw an indexOutOfBounds exception when you try to write to db[2] since there is no memory for this location.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •