First Attempt at Arrays

Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

First Attempt at Arrays

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: First Attempt at Arrays

 
0
  #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 5:36 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: First Attempt at Arrays

 
0
  #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 Quick reply to this message  
Join Date: May 2007
Posts: 4,424
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: First Attempt at Arrays

 
0
  #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 Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: First Attempt at Arrays

 
0
  #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 Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: First Attempt at Arrays

 
0
  #6
Jul 13th, 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 13th, 2007 at 12:26 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: First Attempt at Arrays

 
0
  #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 Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: First Attempt at Arrays

 
0
  #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:
  1. public class Compactdisk
  2. {// begin class
  3.  
  4. // set up array
  5. String cdaName[]= new String[5];
  6.  
  7. //InventoryCD class has 5 fields
  8. String cdName; // cd name
  9. float price; // price of cd
  10. int itemno; // item number of cd
  11. int nstock; // how many units in stock
  12.  
  13. //Compact disk class constructor
  14. public Compactdisk()
  15.  
  16. // 4 fields need to be set up
  17. {
  18. cdName = "";
  19. price = 0;
  20. itemno = 0;
  21. nstock = 0;
  22. }
  23.  
  24. // set values
  25. public void setCDName(String diskName)
  26. {
  27. cdName = diskName;
  28. }
  29. public void setPrice(float cdPrice)
  30. {
  31. price = cdPrice;
  32. }
  33. public void setItemno(int cdItemno)
  34. {
  35. itemno = cdItemno;
  36. }
  37. public void setNstock(int cdStock)
  38. {
  39. nstock = cdStock;
  40. }
  41.  
  42. // return values
  43. public String getCDName()
  44. {
  45. return (cdName);
  46. }
  47. public float getPrice()
  48. {
  49. return (price);
  50. }
  51. public int getItemno()
  52. {
  53. return (itemno);
  54. }
  55. public int getNstock()
  56. {
  57. return (nstock);
  58. }
  59.  
  60. // returns inventory value
  61. public float getValue()
  62. {
  63. return(price * nstock);
  64. }
  65.  
  66.  
  67. }// 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 Quick reply to this message  
Join Date: May 2007
Posts: 4,424
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: First Attempt at Arrays

 
0
  #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 Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: First Attempt at Arrays

 
0
  #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
  1. 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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC