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()
		{	
		[B]return cdName();[/B]
		}
		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
Member Avatar for iamthwee

hint:is that how you declare an array in java? Or is it an array

String cdName[];

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.

Take another look at the line you have bolded - the return line. Does it match any variable in your program?

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?

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:

public class Compactdisk
{// begin class

	// set up array
	String cdaName[]= new String[5];

	//InventoryCD class has 5 fields
	String cdName; //  cd name
	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 = "";
		price = 0;
		itemno = 0;
		nstock = 0;
		}
		
		// set values
	   public void setCDName(String diskName)
	   {
	   cdName = diskName;
	   }
		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

Now if I can effectivley impliment and use this array is another story altogether. We will see.

Why do you wish to use an array for cd name? A cd only has a single name.

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

String cdaName[]= new String{cdName, price, itemno, nstock};

but I cannot find any examples of such a beast for me to learn and build off of.

Member Avatar for iamthwee

>but I cannot find any examples of such a beast for me to learn and build off

There are so many, are you sure you have looked hard enough?

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

String cdaName[]= new String{cdName, price, itemno, nstock};

but I cannot find any examples of such a beast for me to learn and build off of.

Actually, that is not going to help you in the way that you think it might. With the array you have, you will have string entries for the name of each field - but no values for them. Now, you could use a two dimensional array to allow for the name/value pair, but how does that make your class more functional? It doesn't. It just makes it more confusing to use.

In short, there is no compelling reason for you to combine all your variables into an array at this point. Keep them as individual properties.

There can sometimes be value in using a hash table or array for multiple properties like this, but at your current level of programming you are only making things more complicated for yourself. Strive for clear and easy to manage code and leave the tricky stuff for later - and even then, only if it's called for.

Unfortunatley for me it is called for. I must impliment an array into my code for next week's assignment.
With my limited experience (all of 5 weeks now) and even more limited understanding I am just not sure of the mechanics required to do this. i understand what an array is, how it is used, and what all it can do; I just have no real detailed knowledge on how to code it in to my program.
I feel as if I have been instructed on what a wireless access device is, and what it is used for, and then been told to properly install it in my house so that all my existing home devices will use it properly. I think I know how to use it, but implimentation is another story.

So I am stuck at stage 1, impliment an array.

I have not been instructed on if an array replaces my current variable set up, does it append to and utilize those variables, or does it even operation on its own in another class entirely, so all this is a mystery to me.

What class uses the CD class? A person or store may have many CDs and that would be an appropriate use for an array.

Compactdisk[] myCDs = new Compactdisk[100];

I have the compactdisk class you see above, and then the inventory class here.

import java.util.Scanner; //uses class Scanner

public class Inventory
{// begin class Inventory
	public static void main(String[] args)	
	{//begin method main
	
	Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class
	
	Scanner input = new Scanner(System.in);  // create scanner
	
	
			// begin display method
			System.out.print("Enter CD Name or STOP to Exit: ");
			thisCompactdisk.setCDName(input.next());  // read cd name
			
		  	while (!thisCompactdisk.getCDName().equalsIgnoreCase("STOP"))
			{// begin main While

				System.out.print("Enter Price of this CD: "); // prompt for price
				thisCompactdisk.setPrice(input.nextFloat());	// price input from user.
				while (thisCompactdisk.getPrice()<= 0)
				{// begin while
				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
				thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
				} // End while
		
				System.out.print("Enter CD Item Number: "); // prompt for cd item number
				thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
		
				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
				thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
				
        
				System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
				System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
				System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
		
				System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
				thisCompactdisk.setCDName(input.next()); //name input from user
						
			} // End main While
		System.out.print("Ending Program.");
	

	}// end method main
} // end class Payroll

This is where I start getting confused, which class should my array be defined in? Should all my get and sets be discarded as a result? If so, then what will prompt me to enter information that will be stored in the array?

Try to use the array in the inventory and then add CD's to the array during each loop.

Think about how the different entities interact. An inventory is simply a collection of things. Each of those things have properties such as a name, price, etc.

If you were using an inventory, what would it need to do? Perhaps list it's items, add a new item, remove an item. Arrays and other things like vectors allow you to manage a collection of things.

The things in the collection have various properties that describe them. To interact with each one, they provide methods to let you get or set their various properties and perhaps they may have other functions they can do. These methods define what you can do with a type of thing.

Keeping that in mind, consider how your inventory would keep track of many CDs, add a new CD, etc. A new CD object does not have any definition (or "state") until you set those properties on it.

I hope that helps a bit. I'm being a bit vague on purpose so that you will consider the parts of your program and how they should interact. Try to build them to act as real world entities, each with their own state and abilities.

To explain using a different example: imagine a queue of people each person has unique attributes e.g. name, age, address etc (just like each cd would). Each time a person joins the queue their attribute must be known and when they are they can then be added to the queue.

That is what I am trying to do, but my coding inadequacies keep me from putting down in a program what I know needs to be done.
I know the array should accept the information each time the loop is executed, store it, and let me manipulate it later, I just do not know how to get it to do so.
For instance, just to start the array, I will attempt to put it in my Inventory class. It has to be
String cdaname[cdname, price, itemno, nstock] = new String [5];
should create an array named cdaname, that will accept the given information for 5 cds. (I think). But not all that information is string information. Price is float, itemno is int, so will that one array accept all that information? and will it allow me to total the price later?

If I want to make an array that has the numbers 1,2,3,4,5 in it, that is easy.
Int arrayname[] = {"1", "2", "3", "4", "5"};

My questions are 3,
1. If I make it "String arrayname" can I put numbers in there also?
2. How can I change that 1,2,3,4,5, to variables to be input by the user?
It seems I should start with
String arrayname[] = new String [5]; (because I want to do 5 cds) ... but I do not know where the variable names go? If they do not get entered into the array, then
3. how would I put the array into the Inventory program so that it knew to store them there?

Does that help?

What i think you need to do is implement an array of a user defined class. Here is an example...

A class Shape with two attributes, which are set by the constructor and two methods to access those attributes.

public class Shape{
    
    String type;
    int sides;
    
    public Shape(String type, int sides){
        this.type = type;
        this.sides = sides;
    }
    
    public String getType(){
        return type;
    }
    
    public int getSides(){
        return sides;
    }
}

Then a class that instanciates two 'shapes' and puts them into an array of shapes.

public class GetShapes{
    Shape[] shapes;
    
    public GetShapes(){
        shapes = new Shape[2];
        
        //instantiate shapes
        Shape shapeOne = new Shape("Square", 4);
        Shape shapeTwo = new Shape("Triangle", 3);
        
        //add to array
        shapes[0] = shapeOne;
        shapes[1] = shapeTwo;        
        
        //output contents of array
        System.out.print(shapes[0].getType());
        System.out.println(" have " + shapes[0].getSides() + " sides");
        System.out.print(shapes[1].getType());
        System.out.println(" have " + shapes[1].getSides() + " sides");
    }
    
    public static void main(String[] args){
        GetShapes gs = new GetShapes();
    }
}

Hope that helps.

You simply need an array of Compactdisk objects.

Compactdisk[] cds = new Compactdisk[5];

You can add a cd like so

cds[0] = new Compactdisk();

and you can still access the object by array reference like so:

cds[0].getName();

When working in a loop, you will want to use a counter variable for the array index:

int cdCount=0;
String nameInput = input.next();
while (!nameInput.equalsIgnoreCase("STOP")){
   cds[cdCount] = new Compactdisk();
   cds[cdCount].setName(nameInput);
... // set other properties

   cdCount++;
   System.out.print("Enter CD Name or STOP to Exit: ");
   nameInput = input.next();
} // end of while loop

Does that make more sense? You actually store the Compactdisk objects in the array, then you can access any method you want on them.

That does make sense. I had the count worked out in my head, but could not figure out how the array statements worked in to the while loop.
I did some testing last night on it, tried coding all this morning and think I have it mostly worked out. Take a look.

import java.util.Scanner; //uses class Scanner

public class Inventory
{// begin class Inventory
	public static void main(String[] args)	
	{//begin method main
	
	Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class

	// create cd Array
	Compactdisk[] cds = new Compactdisk[5];

	cds[0] = new Compactdisk(); // adds to array
	
	
	Scanner input = new Scanner(System.in);  // create scanner
	
	
			// begin display method
			System.out.print("Enter CD Name or STOP to Exit: ");
			thisCompactdisk.setCDName(input.next());  // read cd name
			
			int cdCount=0;
			String nameInput = input.next();
		  	while (!nameInput.equalsIgnoreCase("STOP"))
			{// begin main While
			
				cds[cdCount] = new Compactdisk();
				cds[cdCount].setName(nameInput);

				System.out.print("Enter Price of this CD: "); // prompt for price
				thisCompactdisk.setPrice(input.nextFloat());	// price input from user.
				while (thisCompactdisk.getPrice()<= 0)
				{// begin while
				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
				thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
				} // End while
		
				System.out.print("Enter CD Item Number: "); // prompt for cd item number
				thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
		
				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
				thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
				
        
				System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
				System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
				System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
		
				cdCount++;
				System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
				nameInput = input.next(); //name input from user
						
			} // End main While
		System.out.print("Ending Program.");
	

	}// end method main
} // end class Payroll

If I follow what you are telling me, then I will not need the Compactdisk class I had created before at all now, right? The values will all be stored in the array itself. I have left it in at this point just to avoid errors, I want to work on one variable at a time so that I can better understand the logic.
If this is all right, the only thing I am still foggy on is the
cds[0].getName(); and
cds[cdCount].setName(nameInput);
statements.
These will be taking the place of my set and get statements in my old Compactdisk class right? This is what will call and store the values in my array. Right now when I put them in the code they error out, I have left the setName where I put it, I think that is right. I am not sure where the getName goes and how it should be set up. Would this be a reason to keep and modify my old Compactdisk class? I think I should have a set and get for each element in my new array, but do not know how to properly set them up.

Cerberos - thank you for that tip. If I understand it correctly, it will still have me setting up the information inside the array myself and then just calling those values later.
What I am trying to do is more like what Ezzaral has suggested, I want the user to enter the values, and my array store and use that information.
I do appreciate your help though. Thanks again.

CIf I understand it correctly, it will still have me setting up the information inside the array myself and then just calling those values later.

Yes you would need to implement some kind of loop to set up each object (and hence the array) with user defined data.

That is what I have spent most of the morning trying to accomplish, building off Ezzaral's suggestion.
At this point I am just not sure how or where to set up the get and set commands. I think I have wrapped my brain around most of the rest of it.

That is what I have spent most of the morning trying to accomplish, building off Ezzaral's suggestion.
At this point I am just not sure how or where to set up the get and set commands. I think I have wrapped my brain around most of the rest of it.

Both the get and set commands would belong to the class which would be instantiated to represent CD's.

Got it ... I think.
I put the Compactdisk class back in, and just modified it for the new array ... at least that is what I think I did.
It looks good, it runs well ( a few bugs to work out, but I think I have it)
Tell me what you think Ezzaral, I might have to put your name as a reference in this somewhere if I ever get it working! :o)
Just look at the name variable right now, that is all I have implimented at this point. Here is the main class.

import java.util.Scanner; //uses class Scanner

public class Inventory
{// begin class Inventory
	public static void main(String[] args)	
	{//begin method main
	
	Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class

	// create cd Array
	Compactdisk[] cds = new Compactdisk[5];

	cds[0] = new Compactdisk(); // adds to array
	cds[0].getName();
	
	int cdCount=0;

	
	Scanner input = new Scanner(System.in);  // create scanner
	
	String nameInput = input.next();
	
			// begin display method
			System.out.print("Enter CD Name or STOP to Exit: ");
			nameInput = input.next();  // read cd name
			
			
		  	while (!nameInput.equalsIgnoreCase("STOP"))
			{// begin main While
			
				cds[cdCount] = new Compactdisk();
				cds[cdCount].setName(nameInput);

				System.out.print("Enter Price of this CD: "); // prompt for price
				thisCompactdisk.setPrice(input.nextFloat());	// price input from user.
				while (thisCompactdisk.getPrice()<= 0)
				{// begin while
				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
				thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
				} // End while
		
				System.out.print("Enter CD Item Number: "); // prompt for cd item number
				thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
		
				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
				thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
				
        
				System.out.print("CD "+thisCompactdisk.getName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
				System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
				System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
		
				cdCount++;
				System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
				nameInput = input.next(); //name input from user
						
			} // End main While
		System.out.print("Ending Program.");
	

	}// end method main
} // end class Payroll

and I am still using this class for my set and gets. See anything I could do to make it smoother? Or am I just fooling myself and this is not using my array at all like I think I am?

public class Compactdisk
{// begin class

	//InventoryCD class has 5 fields
	String Name; //  Name of cd
	float price; // price of cd
	int itemno; // item number of cd
	int nstock; // how many units in stock	
	int cdCount; // cd counter for array
	
	//Compact disk class constructor
	public Compactdisk()
	
		// 4 fields need to be set up
		{ 
		Name = "";
		price = 0;
		itemno = 0;
		nstock = 0;
		cdCount = 0; 
		}
		
		// set values
	   public void setName(String diskName)
	   {
	   Name = diskName;
	   }
		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 getName()
		{	
		return (Name);
		}
		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

Thanks again for all your time and patience. I am sure I am not through testing it, but I have learned more from you over these last two weeks than I have from my online class in 6.

to store inventory, i recommend you to use some kind of database. In java you can use JDMC i think it's called. or, save them in a file if you don't want it to be too much complicated

Programmers Talk can you please read what topic is about in the future? It clearly states "First Attempt at Arrays" !
no1zson is learning about use of array and you just jump miles ahead of what he is trying to do...

no1zson, nice job. It is join to find on the forum somebody who willing to learn and not only expecting solutions from us. Keep going, you doing right!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.