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 ()); }
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
Post a Comment