Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error with this array?  RSS feed

 
Cougar Vis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


these are the error I keep getting


[ May 14, 2004: Message edited by: GK Leo ]

[ May 14, 2004: Message edited by: GK Leo ]

[ May 14, 2004: Message edited by: GK Leo ]
[ May 14, 2004: Message edited by: GK Leo ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared a variable called cArray, which is an array of customers, but there is nothing in it. It's similar to addressing an envelope, but there is no paper inside it.

you need to tell the JVM how many customers you want in your array...


then you can assign to the individual elements of the array...


what the compiler is really saying here though is that what you've written makes no sense. cArray[] by itself makes no sense - you need to refer to a specific element in your array (which you don't have yet - see above) to put something in, or just refer to the whole thing, cArray (withou the brackets) if you want to have it refer to something (i.e. cArray = new Customer[10]
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GK Leo,

To create an empty array you use the following format:

datatype arrayname [];

or

datatype [] arrayname:

Example:

Customer carray [];

or

Customer [] carray;

However, this line only creates an empty array, it does not specify the initial size or values of the array. To create an array and give it initial values you would use the following format:

datatype arrayname [] = new datatype {List element values here seperated by commas};

or

datatype [] arrayname = new datatype {List element values here seperated by commas};

Example:

String CustomerAddress = new String {"Bob", "Joe", "Sally", "Peter"};

Also the arguments you supply seem to be header values to columns in a table. An array would typically store the values that would appear in the cells of a table, not the names of the columns or rows in a table. In my above example I have created an array that contains string values representing customer names. (Arguments that supply the initial values of an array should be encased in squiggly brackets, not square brackets.)

You can store an array of Customer objects that contains variables containg the customer information, but you might consider a collection instead.

You can check out these tutorials for more info on arrays and collections:

Collections Tutorial:
http://java.sun.com/docs/books/tutorial/collections/

Array Tutorial:
http://java.sun.com/docs/books/tutorial/java/data/arrays.html

Landon

[ May 14, 2004: Message edited by: Landon Blake ]
[ May 14, 2004: Message edited by: Landon Blake ]
 
Cougar Vis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed this step:
cArray[0] = new Customer(blah, blah, blah...)


and when i caomplie it again, it said:

A:\Coursework\Customer.java:190: cannot resolve symbol
symbol : constructor Customer (java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Customer
cArray [0] = new Customer (cFirstName, cLastName, cAddress,
^

[ May 14, 2004: Message edited by: GK Leo ]
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by GK Leo:
I followed this step:


[ May 14, 2004: Message edited by: GK Leo ]


Check that you have an appropriate constructor for Customer with the same number of parameters.
i.e. if you are calling cArray[0]=new Customer("Ginger","Rodgers");
then you need to make sure that there is a constructor in your Customer class:

Customer(String firstName, String lastName){}
 
Cougar Vis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My contructor like this:

public Customer(String cFirstNameIn, String cNameIn, String cAddressIn,
String cNameIn, String coAddressIn, int cIDIn, char cGenderIn)


{
customerFirstName = cFirstNameIn;
customerLastName = cLastNameIn;
customerAddress = cAddressIn;
companyName = cNameIn;
companyAddress = coAddressIn;
customerID = cIDIn;
customerGender = cGenderIn;
}


and my arrays like this:


public static void main (String [] args)

Customer [] cArray = new Customer[1];

cArray [0] = new Customer (cFirstNameIn, cLastNameIn, cAddressIn,cNameIn, coAddressIn);


what's wrong with this, o...btw this array is used in OO programming

[ May 14, 2004: Message edited by: GK Leo ]
[ May 14, 2004: Message edited by: GK Leo ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the constructor you posted takes 5 strings, an int and a char (for a total of 7 arguments).

the call you make only passes it 5 strings. you need to either a) pass in an int and a char, or b) make another contsructor that only take 5 strings.
[ May 14, 2004: Message edited by: fred rosenberger ]
 
Wai Hung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the signature of your constructor is

public Customer(String cFirstNameIn, String cNameIn, String cAddressIn,
String cNameIn, String coAddressIn, int cIDIn, char cGenderIn)

However, the error message said

A:\Coursework\Customer.java:190: cannot resolve symbol
symbol : constructor Customer (java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Customer

If you compare the parameters list (5 strings, 1 int and 1 char) in the contructor and that of the one (5 strings only) in the error message, you will find they are different.
This means you are calling the constructor with incorrect parameters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!