Results 1 to 5 of 5
Thread: Array of objects
- 12-02-2012, 02:12 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Array of objects
Hello there
.I am new to java programming and i need help with creating an array of objects.The related codes are the following:
public class Shop {
public static final int NUM_CASHIERS=10;
Cashier[] totalcashiers=new Cashier[NUM_CASHIERS];
for(int j=0; j<=1; j++) {
totalcashiers[j]= new Cashier(true);
}
public class Cashier {
private boolean open;
private ClientQueue queue;
Client currentClient;
int serviceTime;
int idleTime;
public Cashier(boolean open) {
this.open=open;
}
I have to create an array of the class Cashier in the class shop.The max size of the array is 10.In the beginning , i have to create two cashier objects that are open and that is what i am trying to do in the for loop.The error that i need help with is in the 4th line of the class Shop.
error: illegal start of type
for(int j=0; j<=1; j++) {
error: ')' expected
for(int j=0; j<=1; j++) {
error: illegal start of type
for(int j=0; j<=1; j++) {
error: <identifier> expected
for(int j=0; j<=1; j++) {
error: ';' expected
for(int j=0; j<=1; j++) {
error: illegal start of type
for(int j=0; j<=1; j++) {
error: <identifier> expected
for(int j=0; j<=1; j++) {
error: <identifier> expected
for(int j=0; j<=1; j++) {
error: illegal start of type
for(int j=0; j<=1; j++) {
error: <identifier> expected
for(int j=0; j<=1; j++) {
error: ';' expected
Any help is appreciated.Thanks in advance.
- 12-02-2012, 02:20 AM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
Re: Array of objects
hey
i think its because its not in a method as such its just there. i opened the code and it wasnt formatted well.
try this:
Java Code:public class Shop { private static final int NUM_CASHIERS=10; public void create() { Cashier[] totalcashiers=new Cashier[NUM_CASHIERS]; for(int j=0; j<=1; j++) { totalcashiers[j]= new Cashier(true); } } }Last edited by monkeyjr97; 12-02-2012 at 02:24 AM.
- 12-02-2012, 02:28 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Re: Array of objects
It worked!Thanks a lot :) Can you explain why it had to be in a method?I would never think that that was the reason.As i already said, i am new to java programming :P
- 12-02-2012, 02:37 AM #4
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
Re: Array of objects
has to be inside a {} block so it can actually be compiled and understood
you cant just have pieces of your code dotted around the .java file. :)
- 12-02-2012, 02:45 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Array of objects
By EnSlavingBlair in forum New To JavaReplies: 4Last Post: 09-30-2011, 01:27 PM -
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
help with and array of objects
By hoosierfan24 in forum New To JavaReplies: 5Last Post: 01-23-2011, 02:45 AM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks