I have just been using a standard String to input and return information. I am trying to impliment an Array in to this inventory program so that I can store and view more than one item at a time.
I think I have done ok until I get to the Return part of the program. I have tried several different things but nothing seems to work.
As it is, I am getting a "cannot find symbol" error message on "cdName" in the return statment. Any help on what I am doing wrong?
public class Compactdisk
{// begin class
//InventoryCD class has 5 fields
String cdName[]; // cd name array
float price; // price of cd
int itemno; // item number of cd
int nstock; // how many units in stock
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
cdName = new String [25];
price = 0;
itemno = 0;
nstock = 0;
}
// set values
public void setCDName(String diskName)
{
cdName = cdName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
// return values
public String getCDName()
{
return cdName();
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
// returns inventory value
public float getValue()
{
return(price * nstock);
}
}// end class
I did have it all in one line to begin with, (declaring and creating)
String cdName[] = new String [25]
but it is easier for me to read this way, and I get the same error either way.
Those are the only two ways I have learned to do this, unless I am just way off base right now and doing something wrong.
I am really starting to feel dumb. I am either missing something right in front of my face, or completely do not understand the question. I see what I think is the variable name several times, through the setup, initialization and everything.
//InventoryCD class has 5 fields
String cdName[]; // cd name array
// 4 fields need to be set up
cdName = new String [25];
// set values
public void setCDName(String diskName)
{
cdName = cdName;
I tried diskName as well, just because I was so frustrated, but same error.
cdName() is trying to call a function called "cdName". Elements of string are referenced using [...] (if that is what you want to do). Also, your setter method is pretty much useless, I think you mean cdName = diskName?
Last edited by sillyboy; Jul 13th, 2007 at 12:26 am.
I had cdName = diskName when I first started implimenting this array. That line gave me an "incompatible types" error. Changing it to what it is now enabled me to move on to the next and current error.
The problem appears to be that I was trying to set up and array to take the place of cdName, and that is not what I really wanted to do.
I want an array to use cdName. So had gone through and tried to replace all my cd name variables, gets, and sets with array instances and it was just confusing itself. I put my code back the way it was, added an array and that seems to have gotten the first step done.
Code now looks like this:
That was part of my misunderstanding. I was going to create an array for each variable. (I thought each variable would be a different type and require its own array).
Now I understand that I can use a single array for all field types. I just left in named cdaName because that made the most sence to me, it will be an array of cd information.
My problem now is that I do not understand where to put this array. I want it to be populated with information input from the user, so I think I need to keep all my get and set code, but where does my array tie in? Do I need to create another class completley?
I think my array should look something like
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.