java - Array indexing issues -



java - Array indexing issues -

i trying utilize loop create 7 instances of class. can go through loop 7 times, 1 time press come in arrayindexoutofbound error.

my code follows:

data[] temperatures = new data[7]; for(int i=1; i<=temperatures.length + 1; i++) { system.out.println("please come in temperature day " + i); temperatures[i] = new data(input.nextdouble()); }

array indexes start @ 0. problem code trying access 8th array element doesn't exist. more information, refer the java™ tutorials - arrays.

your code should this:

data[] temperatures = new data[7]; // indexes 0-6 (makes total of 7 indexes) // start loop index 0, end index 6 (= temperatures.lenght) (int = 0; < temperatures.length; i++) { // since sound unusual come in temperature day 0, notice (i+1) system.out.println("please come in temperature day " + (i+1)); temperatures[i] = new data(input.nextdouble()); }

java arrays indexoutofboundsexception

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -