Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2010, 12:00 PM   PM User | #1
yotamoo
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
yotamoo is an unknown quantity at this point
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!
yotamoo is offline   Reply With Quote
Old 09-22-2010, 02:11 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:34 PM.


Advertisement
Log in to turn off these ads.