Thread: Array question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    4
    Rep Power
    0

    Array question


    Hi all. I'm relatively new to Java and working through the Oracle docs. I'm trying to do a simple array, but NetBeans is giving me a fairly generic error about needing a "]". Can someone please tell me what is wrong with the following syntax?

    //Array declaration with size
    int[] wheelCount = new int[3];

    wheelCount[0]=0;
    wheelCount[1]=1;
    wheelCount[2]=2;
    wheelCount[3]=3;


    Thank you
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Can you indicate where the error is? Which statement(s) are flagged as having errors?
    What column or character in those statements is flagged with the error.

    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    BTW Array index values range from 0 the the array length-1;
    Last edited by NormR; August 17th, 2013 at 06:22 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    4
    Rep Power
    0
    Sorry Norm, I didn't give much detail did I? Netbeans doesn't appear to give a position like the javac does. Thanks for the pointer regarding the index count being -1, I hadn't caught that issue yet.

    Here's what javac generates.
    Code:
    mybbicycles.java:30: error: ']' expected
        wheelCount[0]=0;
                        ^
    mybbicycles.java:30: error: ';' expected
        wheelCount[0]=0;
                          ^
    mybbicycles.java:30: error: illegal start of type
        wheelCount[0]=0;
                           ^
    Full code below:
    Code:
    package mybbicycles;
    
    /**
     *
     * @author RobertT
     */
    class Cycle {
    
        int speed = 0;
        int maxGears = 10;
        int minGears = 1;
        int currentGear = 1;
        int wheels;
        String typeName = "Not defined";
        static String shape = "round";  //Count add keyword "final" to prevent from changing
        //Note char data type uses single quotes
        char testing = 'a';
        
        //Note String data type uses double quotes
        String test ="a";    
        //The following line is illegal - string datatypes are immutable
        //test.concat("a");
        
        //Array declaration with size
        int[] wheelCount = new int[4];
        wheelCount[0]=0;
        wheelCount[1]=1;
        wheelCount[2]=2;
        wheelCount[3]=3;
       
        
        void typeOfBike() {
            switch (wheels) {
    
                case 0:
                    typeName = "Not defined";
                    break;
                case 1:
                    typeName = "unicycle";
                    break;
                case 2:
                    typeName = "bicycle";
                    break;
                case 3:
                    typeName = "tricycle";
                    break;                
            }
        }
    
        void setSpeed(int s) {
            speed = s;
        }
    
        void increaseSpeed(int s) {
            speed = speed + s;
        }
    
        void  setWheels(int w) {
            wheels = w;
            typeOfBike();
        }
    
        void tellMeMore() {
            System.out.println("You have a " + typeName + " with " + String.valueOf(wheels) + " wheels.  It's current speed is " + String.valueOf(speed) + ".");
    
        }
    }
    
    class Unicycle extends Cycle {
        Unicycle() {
            super.setWheels(1);
        }
    }
    
    class Bicycle extends Cycle {
        Bicycle() {
            super.setWheels(2);
        }
    }
    
    class Tricycle extends Cycle {
        Tricycle() {
            super.setWheels(3);
            System.out.println("The wheels on the bus go " + Tricycle.shape + " and " + Tricycle.shape);
        }
    }
    public class MyBbicycles {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Cycle c1 = new Cycle();
            c1.setSpeed(10);
            c1.setWheels(2);
            c1.tellMeMore();
            c1.increaseSpeed(5);
            c1.tellMeMore();
            
            Unicycle uCycle= new Unicycle();
            uCycle.tellMeMore();
            
            Bicycle bicycle = new Bicycle();
            bicycle.tellMeMore();
            
            Tricycle tricycle = new Tricycle();
            tricycle.setSpeed(8);
            tricycle.tellMeMore(); 
        }
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    4
    Rep Power
    0
    Correction, the ^ is under the index position zero (first zero) in the line wheelCount[0]=0;
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    The assignment statements need to be inside of a method.

    An array can be initialized with this syntax:
    Code:
    int[] anInArray = {0,1,2,3};
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    4
    Rep Power
    0
    Norm,

    Thank you for the clarification. Let me make sure I understand the implications of this and I don't make the mistake again.

    1. Variables can be defined and simultaneously initialized within the class body, BUT
    2. Variable values can only be assigned/manipulated/changed from within a method once they are defined. Presumably to protect the variable state and follow OOPs principles?

    Thank you for your help and patience.

    Robert

IMN logo majestic logo threadwatch logo seochat tools logo