• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Array  RSS feed

 
Alex Moradi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone

I started learning JAVA with How to program JAVA( Detail & Detail ) E-book;

In one example the book told this Code:





and i want use input and get data from user, and and modified that like below:



when i compile this code i got an error, where is my mistake ?

tnx for your answer.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your responses array has length 5, and you're trying to put result at index 5, which is the 6th element of the array. Recall that array indexing is zero-based.
 
Alex Moradi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Your responses array has length 5, and you're trying to put result at index 5, which is the 6th element of the array. Recall that array indexing is zero-based.


hi,

this is my new code :



i can't enter more than 1 number and after that: ERROR :|
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got weird spacing in your code. Look in this forum how other's write code and try to follow such convention.

Again, problem is the same as been mentioned already.

Now you are trying to store result at index 10 (line 18), while you have declared array of length 10 (line 9). That means, your highest index (of the last element) is 9.

That is because:
int[] A = new int[4];

A[0] - first element
A[1] - second element
A[2] - third element
A[3] - fourth (last) element
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!