Im working on a simple array that takes 3 values then prints the values and sums them ,but I keep getting this int cannot be dereferenced error

//This program demonstrates the use of arrays in storing data.
//The student will enter and process data according to the

import javax.swing.JOptionPane;

public class Array{

      public static void main ( String args[] ){
            final int SIZE = 3;                        //The size of our array
            int array1[] = new int[SIZE];      //Array declaration
            String tempHolder;                        // temporary holder for String input
            //Prompt to enter 'SIZE' number of integers
      for( int i = 0; i < SIZE; i++ )
            tempHolder = JOptionPane.showInputDialog("Enter an integer: ");
            array1[i] = Integer.parseInt(tempHolder);

String outputString = "";

      for( int i = 0; i < array1[i].length; i++ )
            outputString = outputString + "Index " + i;
            outputString += " contains value: " + array1[i] + "\n";
JOptionPane.showMessageDialog ( null, outputString);

Who is Participating?
ADSLMarkConnect With a Mentor Commented:
The variable array1 is a single dimension array. If you want to get the length of this array you should do:


In the for loop you do:


which is read as:

array1[i] (=i-th element in array1, which results in an integer (int))
and then you want to access attribuut .length of an integer (which is not possible)

So just remove the [i] and it should be working.
Also it might not be preferable to name your class Array (without putting it into another package) since Array is a default class in the Java package.

> for( int i = 0; i < array1[i].length; i++ )
should be

for (int i = 0; i < array1.length; i++) {
ddurmonConnect With a Mentor Commented:
ADSLMark is correct. A better coding standard to use is as follows:

int[] array1 = new int[SIZE];

The reason for this is that when reading this code the statement clearly says
An integer array NAMED array1 = a new array of integer size X.

The way you were coding added to your confusion in your last loop because while what you declared is TECHNICALLY correct at the top it reads as if you have a integer named array1 oh and by the way the integer is an array of integers.  It is not WRONG, it is just easier to understand when referencing.  

Take a look at "The Elements of Java Style" book (ISBN 0-521-77768-2) on page 26 example 27.  While not specifically identifying the array question, you will notice that they do as I have recommended.  

ADSLMark is correct in that if you do as he has stated it will fix your "int can not be dereferenced error".

To avoid making similar mistakes in the future I would recommend declarations be in the following format:

for example:
int[] array1 = new int[10];
char[] myText = new char[1024];

Hope this helps and your assignment gets an A :)

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

I would change the name Array to myArray or something else.

I believe Object.Array is defined in Java.
> I believe Object.Array is defined in Java.

Not defined. please please do *not* to post comments if you are not sure.
Ajay-SinghConnect With a Mentor Commented:
> How about: java.lang.reflect.Array

Yes. In fact there are be other third party Array classes as well. My point was there is nothing called "Object.Array" in java - there is no field (or static class) named "Array" in java.lang.Object class.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.