User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,563 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,738 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2749 | Replies: 37 | Solved
Reply
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

First Attempt at Arrays

  #1  
Jul 12th, 2007
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: First Attempt at Arrays

  #2  
Jul 12th, 2007
hint:is that how you declare an array in java? Or is it an array

String cdName[];
Last edited by iamthwee : Jul 12th, 2007 at 4:36 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #3  
Jul 12th, 2007
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,599
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 11
Solved Threads: 257
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: First Attempt at Arrays

  #4  
Jul 12th, 2007
Take another look at the line you have bolded - the return line. Does it match any variable in your program?
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #5  
Jul 12th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: Mar 2007
Posts: 120
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
sillyboy sillyboy is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #6  
Jul 12th, 2007
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 12th, 2007 at 11:26 pm.
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #7  
Jul 13th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #8  
Jul 13th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,599
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 11
Solved Threads: 257
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: First Attempt at Arrays

  #9  
Jul 13th, 2007
Why do you wish to use an array for cd name? A cd only has a single name.
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First Attempt at Arrays

  #10  
Jul 13th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:02 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC