Solved

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

Posted on 2006-11-27
9
203 Views
Last Modified: 2010-03-31
/****************************************************************/
//Program:Array.java
//Date:8/06/03
//
//This program demonstrates the use of arrays in storing data.
//The student will enter and process data according to the
//exercises.
/****************************************************************/

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);
      

            System.exit(0);
      }
}
0
Comment
Question by:lusius188
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18020196
> for( int i = 0; i < array1[i].length; i++ )
should be


for (int i = 0; i < array1.length; i++) {
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 84 total points
ID: 18020204
The variable array1 is a single dimension array. If you want to get the length of this array you should do:

array1.length

In the for loop you do:

array1[i].length;

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.

Mark
0
 

Assisted Solution

by:ddurmon
ddurmon earned 83 total points
ID: 18025310
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:
<TYPE>  <VARNAME>  [= <INITIALIZATION>]

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

Hope this helps and your assignment gets an A :)

Dwight
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:SamsonChung
ID: 18029171
I would change the name Array to myArray or something else.

I believe Object.Array is defined in Java.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18030724
> I believe Object.Array is defined in Java.

Not defined. please please do *not* to post comments if you are not sure.
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18030742
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 83 total points
ID: 18030816
> 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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 41
restrict decimal places for double datatype 10 32
Running JavaFX on the Raspberry Pi 27 137
Java array 21 86
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question