Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Arrays Index out of Bounds

Tags:

java

arrays

I'm just starting a unit in Arrays and I was given some example code to look off of for this very basic introductory program for Arrays. Essentially all I have to do is make two arrays that ask for the temperature for that day of the week. After collecting the info it will simply spit it back out in a string like this.

The temperature on Monday was 16 degrees

The temperature on Tuesday was 18 degrees

... etc.

From what I understood from the example code I received I am doing everything correctly. But when try to run the program (in Netbeans) I get this error.

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at temperatures.Temperatures.main(Temperatures.java:27) Java Result: 1"

Here is the code:

public static void main(String[] args)throws IOException {
        // TODO code application logic here
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        String temp[]= new String[7];
        String day[]= new String[7];
        day[1]=("Monday");
        day[2]=("Tuesday");
        day[3]=("Wednesday");
        day[4]=("Thursday");
        day[5]=("Friday");
        day[6]=("Saturday");
        day[7]=("Sunday");
        for (int i=0; i <7; i++){
            System.out.println("Please enter the temperature for" + day[i]);
            temp[i]=br.readLine();
        }
        for (int i=0; i <7; i++){
        System.out.println("The high temperature on " + day[i]+ " was "+ temp[i]);
        }
    }
}
like image 1000
Dave555 Avatar asked May 17 '12 17:05

Dave555


People also ask

How do you check if an array index is out of bounds?

Another way of checking if an array is out of bounds is to make a function. This will check if the index is "in bounds". If the index is below zero or over the array length you will get the result false. This should be built in as a member function of the array class..

What does index 0 out of bounds for length 0 mean in Java?

If the length of the array (which is the number of elements) is zero, then there are no valid numbers you can use as an index - so any access to the array with an index will give you that error.

What will happen if we use out of bounds index in an array?

If, we use an array index which is out of bounds, then the compiler will compile and even run. But, there is no guarantee for the correct result. Result can be not sure and it will start causing many problems.


3 Answers

Arrays begin at zero (<- that's a link to an article which explains why). So assigning your first value as day[1]=("Monday"); is the issue, it should be day[0]=("Monday"); Hope that helps

like image 128
kentcdodds Avatar answered Oct 26 '22 18:10

kentcdodds


Arrays in Java start at position 0, not position 1. So if you initialize it to a size of 7, Monday is 0 and Sunday is 6. There is no index 7 available.

like image 20
Affe Avatar answered Oct 26 '22 19:10

Affe


The arrays starter in 0,

try this

    day[0]=("Monday");
    day[1]=("Tuesday");
    day[2]=("Wednesday");
    day[3]=("Thursday");
    day[4]=("Friday");
    day[5]=("Saturday");
    day[6]=("Sunday");

and should you change this loop

for(int i=0; i < 7; i++){
        System.out.println("Please enter the temperature for" + day[i]);
        temp[i]=br.readLine();
    }

for this

for(int i=0; i < day.length(); i++){
        System.out.println("Please enter the temperature for" + day[i]);
        temp[i]=br.readLine();
    }

I hope help you.

like image 33
hekomobile Avatar answered Oct 26 '22 18:10

hekomobile