Results 1 to 6 of 6

Thread: Array question

  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Array question

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package testing;
    
    /**
     *
     * @author Sterling
     */
    public class TypeConversion {
    
        private double array[] = new double[3];
        private double sum;
        private double divisor = 4;
        private double average;
           
        array[0] = 4;
        array[1] = 3;
        array[2] = 15;
        array[3] = 8;
        
        sum = array[0]+array[1]+array[2]+array[3];
        average = sum/4;
        
        System.out.println(average);
        
        
        
    
        
        
    
        
    
    }
    Netbeans has 20 errors listed. When I compiled it listed this...


    Java Code:
    init:
    deps-jar:
    Created dir: C:\Users\Sterling\Documents\NetBeansProjects\Testing\build\classes
    Compiling 1 source file to C:\Users\Sterling\Documents\NetBeansProjects\Testing\build\classes
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:19: ']' expected
        array[0] = 4;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:19: ';' expected
        array[0] = 4;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:19: illegal start of type
        array[0] = 4;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:19: <identifier> expected
        array[0] = 4;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:20: ']' expected
        array[1] = 3;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:20: ';' expected
        array[1] = 3;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:20: illegal start of type
        array[1] = 3;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:20: <identifier> expected
        array[1] = 3;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:21: ']' expected
        array[2] = 15;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:21: ';' expected
        array[2] = 15;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:21: illegal start of type
        array[2] = 15;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:21: <identifier> expected
        array[2] = 15;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:22: ']' expected
        array[3] = 8;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:22: ';' expected
        array[3] = 8;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:22: illegal start of type
        array[3] = 8;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:22: <identifier> expected
        array[3] = 8;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:24: <identifier> expected
        sum = array[0]+array[1]+array[2]+array[3];
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:25: <identifier> expected
        average = sum/4;
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:27: <identifier> expected
        System.out.println(average);
    C:\Users\Sterling\Documents\NetBeansProjects\Testing\src\testing\TypeConversion.java:27: <identifier> expected
        System.out.println(average);
    20 errors
    BUILD FAILED (total time: 0 seconds)
    The program looks right to me...I don't get what the compiler is trying to tell me.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Your problem is the following:
    You created an array of 3 elements (0,1,2):
    Java Code:
    private double array[] = new double[3];
    But you're trying to put 4 elements into it. Make the array of 4 elements and at least that problem will go away.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    other than when defining a variable, you can't change one's value outside of a method.

  4. #4
    annoyingzhang is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    I think it's just too many elements.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Another mistake here is, you don't have place all of them in a method. Defining all of them globally. You cannot do this anyway.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default yeap

    Very good catch Eranga... yeah... either you stuff all the calculations in a method (and call that method from another class) or you place in a main method (stand alone class).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. file/ array question
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 02-18-2009, 03:54 PM
  3. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 PM
  4. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM
  5. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

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