java - Array indexing issues -


I'm trying to create 7 classes of a class for a loop. I can go through 7 times the loop, but once I press once I get a ArrayIndexOutOfBound error

My code is as follows:

  data [] temperature = new data [7]; (Int i = 1; i & lt; = temperature.length + 1; i ++) {System.out.println ("Please enter temperature for day" + i); Temperature [i] = new data (input.nextDouble ()); }    

 Java array index

Array starts with indexes 0. The problem with your code is that you are trying to use the 8th array element that does not exist . For more information, see.

Your code should look something like this:

  data [] temperature = new data [7]; // Start the loop with index 0-6 (make 7 indexes total) // index 0, for it index 6 (= temperature lining) (int i = 0; i & lt; temperature; height; i ++ ) {// Since it may seem strange to enter the temperature for day 0, notice (i + 1) System.out.println ("Please enter the temperature for the day" + (i + 1 )); Temperature [i] = new data (input.nextDouble ()); }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -