• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array dismay

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The three lines where you are putting the shirts into the shirtArray are not inside any method.
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Try the following


bye for now
sat
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic