954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

First Attempt at Arrays

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()
		{	
		<strong>return cdName();</strong>
		}
		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
no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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

String cdName[];

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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?

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

>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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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];
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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?

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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?

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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?

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You