java - "Array Index Out of Bounds Exception 6" -


this code works, there's 1 thing. compiled without errors, after tried run it, showed me exception:

 exception in thread "main" java.lang.arrayindexoutofboundsexception: 6 @ javajoe.main(javajoe.java:3) 

this code:

 import java.text.decimalformat;  import java.text.numberformat;   public class javajoe   {   public static void main(string[] args)      {   double money = 200;  double area = 0;  double money1 = 0;  double money2 = 0;  double money3 = 0;   string [] day = {"monday", "tuesday", "wednesday", "thursday",  "saturday", "sunday"};   numberformat decimal = new decimalformat("$###.00");  numberformat decimal1 = new decimalformat("###.0");   (int x = 0; x <= 6; x = x+1)  {     if(day[x].equals("monday"))     {         double totalcost = 30 * 1.15; //cost including tax         money = money - totalcost;         system.out.println("it " + day[x] + " , joe has spend " +     decimal.format(totalcost) + " on new pair of shoes. has " + decimal.format(money) + " left.");      } else if(day[x].equals("tuesday"))     {         area = 12 * 7;         system.out.println("it " + day[x] + ". ceiling joe wants paint " + area + " metres squared.");      } else if(day[x].equals("wednesday"))     {         double price = 1.13 * area; //how money spent on paint per square litre         money1 = money - price;         system.out.println("it " + day[x] + ". joe spends " + decimal.format(price) + " on paint. has " + decimal.format(money1) + " left.");      } else if(day[x].equals("thursday"))     {         double gasprice = 36.40;         double litresgas = gasprice / 0.45; //calculation find how many litres bought         money2 = money1 - gasprice;         system.out.println("it " + day[x] + ". joe spends " +decimal.format(gasprice) + " on gas , buys " + decimal1.format(litresgas) + " litres. has " + decimal.format(money2) + " left.");      } else if(day[x].equals("saturday"))     {         double charity = 23; //money spent on charity         money3 = money2 - charity;          system.out.println("it " + day[x] + ". joe donates " + decimal.format(charity) + " charity. has " + decimal.format(money3) + " left." );      }else if(day[x].equals("sunday"))     {         system.out.println("today " + day[x] + ".");     } //if   } //for   } //main   } //class 

could me out , explain it?

your days array contains 6 elements, meaning indexed 0 5 (inclusive).

your loop should like:

for (int x = 0; x < 6; x = x+1) 

or better:

for (int x = 0; x < day.length; x++) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -