6

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]);
        }
    }
}

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh Okay, I had a feeling it would be something like a minor logical error. I re-ran the program, replacing the array numbers and it worked. Thanks!
Welcome to Stack Overflow :) I'm glad you figured it out! Please accept the answer you feel answered your question in the best way (click the little check mark below the voting arrows) so future comers with similar questions will know what solved your problem.
4

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.

Comments

1

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.

1 Comment

Watch out, this was tagged with homework. We don't want to give someone the answer explicitly, just point them in the right direction.
0

That is because your array starts from 1, ends from 7. If your array length is 7, the last index should be one less than tyhe array length. In your case, array sees that you have declared it for 7 elements, but inserted 8 positions. So, start array from 0, end it from 6

Comments

0

If an Array is is 'N' then the boundaries of that array is 0 and "N-1". In your case the array Boundaries are 0 and 6. But you are tring to write value into array[7] which is not exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.