Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array dismay

 
Bryan Lemmer
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

what is wrong in the following code??

public class ShirtArrayTest
{

Shirt shirt1 = new Shirt(1,"nice shirt1",'R',15.00,60);
Shirt shirt2 = new Shirt(2,"nice shirt2",'R',14.00,50);
Shirt shirt3 = new Shirt(3,"nice shirt3",'R',13.00,40);

Shirt shirtArray[] = new Shirt[3];
shirtArray[0] = shirt1;
//shirtArray[1] = shirt2;
//shirtArray[2] = shirt3;
}

i get a ']' expected and a <identifier. expected. evertthing compiles fine until i put in the line shirtArray[0] = shirt1; i have tried type casting the entry shirtArray[0] = (Shirt)shirt1; but i geteven more errors.

please help i have been staring at this code for 30min and i cant seem to find the problem.

thanks
Bryan
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The three lines where you are putting the shirts into the shirtArray are not inside any method.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try the following


bye for now
sat
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite right. Putting the three lines in a constructor should work nicely.
Glad to be able to help.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will however have problems with your array of shirts.
You have now declared the array as a local variable in the constructor. You want this as a field:-and this slightly changed constructor:-CR
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or an alternative, made by copying and pasting parts of your code:-. . . which will work as long as you are not using "shirt1" as an identifier elsewhere.
 
Bryan Lemmer
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Guys,

placing the code in a method worked fine. my new question now is why does it have to be placed in a method? why doesn't it work the way i have written it?

if you could add to my understanding it would be greatly appreciated.

Bryan
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statements with instructions like that never work outside methods [or constructors]. I am not sure I can explain it better than that.


Anybody else know how to explain it better??

CR
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all code can be thought of as a flow of ideas. much like circuits hold a flow of electrons, and only electrons within the circuit can flow, methods contain the flow of ideas -- of code statements. Only statements within methods have a chance of joining the program flow and being executed.

Anything outside of the methods is out of the flow stream; to use another metaphor, these statements are sitting on the river bank, and there they will sit forever, never to join in with the running of the code statements that flow through the methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic