2013-05-21 4 views
-3

Ce code fonctionne, mais il y a une chose. Il a compilé sans erreurs, mais après avoir essayé de courir, il m'a montré une exception:"Array Index Out of Bounds Exception 6"

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at JavaJoe.main(JavaJoe.java:3) 

Voici le 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"); 

for (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 is " + day[x] + " and Joe has to spend " +  decimal.format(totalCost) + " on a new pair of shoes. He has " + decimal.format(money) + " left."); 

    } else if(day[x].equals("Tuesday")) 
    { 
     area = 12 * 7; 
     System.out.println("It is " + day[x] + ". The ceiling Joe wants to paint is " + area + " metres squared."); 

    } else if(day[x].equals("Wednesday")) 
    { 
     double price = 1.13 * area; //how much money he spent on paint per square litre 
     money1 = money - price; 
     System.out.println("It is " + day[x] + ". Joe spends " + decimal.format(price) + " on paint. He has " + decimal.format(money1) + " left."); 

    } else if(day[x].equals("Thursday")) 
    { 
     double gasPrice = 36.40; 
     double litresGas = gasPrice/0.45; //calculation to find how many litres he bought 
     money2 = money1 - gasPrice; 
     System.out.println("It is " + day[x] + ". Joe spends " +decimal.format(gasPrice) + " on gas and buys " + decimal1.format(litresGas) + " litres. He has " + decimal.format(money2) + " left."); 

    } else if(day[x].equals("Saturday")) 
    { 
     double charity = 23; //money he spent on charity 
     money3 = money2 - charity; 
     System.out.println("It is " + day[x] + ". Joe donates " + decimal.format(charity) + " to charity. He has " + decimal.format(money3) + " left."); 

    }else if(day[x].equals("Sunday")) 
    { 
     System.out.println("Today is " + day[x] + "."); 
    } //if 

} //for 

} //main 

} //class 

Pourriez-vous me aider et expliquer?

+0

http://stackoverflow.com/editing-help#code – SLaks

+2

'String [] day' manque vendredi – Zutty

Répondre

1

tableau Vous est de taille , donc les index sont 0-5.

Utilisation:

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

pour toute longueur du tableau, ou ajouter Friday à String [] day

+1

Merci! Cela fait parfaitement sens. – Salma

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

Votre tableau days contient 6 éléments, ce qui signifie qu'ils seront indexés de 0 à 5 (inclus).

Votre boucle devrait ressembler à:

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

ou mieux encore:

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

index Array commencent toujours par 0, donc s'il y a un élément 7 du tableau, dernier indice d'élément 7-1 sera-à-dire 6.

changer donc votre code:

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

à

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