This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes array dismay Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array dismay" Watch "array dismay" New topic
Author

array dismay

Bryan Lemmer
Greenhorn

Joined: Feb 01, 2007
Posts: 11
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

Joined: Oct 13, 2005
Posts: 37936
    
  22
The three lines where you are putting the shirts into the shirtArray are not inside any method.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi,

Try the following


bye for now
sat
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Quite right. Putting the three lines in a constructor should work nicely.
Glad to be able to help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: Oct 13, 2005
Posts: 37936
    
  22
. . . 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

Joined: Feb 01, 2007
Posts: 11
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

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array dismay
 
Similar Threads
Arraylist set(index i, Object o) method Exception
initializing an array of arrays
stream output variables to specific positions in output file
Question on HashSet
BItwise & logical operators question