Results 1 to 5 of 5
  1. #1
    Stavrosgr is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default 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 03:24 AM.

  3. #3
    Stavrosgr is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default 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. :)

  5. #5
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Array of objects

    Quote Originally Posted by monkeyjr97 View Post
    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. :)
    hey!!!! I actually understood that!!!! XO

Similar Threads

  1. Array of objects
    By EnSlavingBlair in forum New To Java
    Replies: 4
    Last Post: 09-30-2011, 02:27 PM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 04:48 PM
  3. help with and array of objects
    By hoosierfan24 in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 03:45 AM
  4. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 05:03 AM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •