944,028 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 6118
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2007
0

First Attempt at Arrays

Expand Post »
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
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 12th, 2007
0

Re: First Attempt at Arrays

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 12th, 2007
0

Re: First Attempt at Arrays

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 12th, 2007
0

Re: First Attempt at Arrays

Take another look at the line you have bolded - the return line. Does it match any variable in your program?
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 12th, 2007
0

Re: First Attempt at Arrays

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 13th, 2007
0

Re: First Attempt at Arrays

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.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jul 13th, 2007
0

Re: First Attempt at Arrays

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 13th, 2007
0

Re: First Attempt at Arrays

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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 13th, 2007
0

Re: First Attempt at Arrays

Why do you wish to use an array for cd name? A cd only has a single name.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 13th, 2007
0

Re: First Attempt at Arrays

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
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

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.
Message:
Previous Thread in Java Forum Timeline: a thread challenge-----
Next Thread in Java Forum Timeline: Writing Servlets (Maybe)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC