• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Problem with Array  RSS feed

 
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.
 
Saloon Keeper
Posts: 9248
177
  • 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 :|
 
Marshal
Posts: 6008
415
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!