+ Reply to Thread
Results 1 to 2 of 2

Thread: Java Array Issue

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6

    Java Array Issue

    I am creating an array that store week`s days , here is the example i am using :
    Code:
            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]);
            }
    But when i run the code , i get error : java.lang.ArrayIndexOutOfBoundsException.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Arrays index alawys starts from 0 , in your case you started the array from index : 1 , and that is wrong , here is an example :
    your first value is day[1]=("Monday"); is the issue, it should be day[0]=("Monday"); till day[6]=("Friday");

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts