2017-03-29 3 views
-3

J'écris un programme qui prend à peu près une série de nombres et d'opérateurs, les divisant, puis faisant les calculs dessus. J'ai quatre instructions IF séparées pour chaque opérateur dans une boucle For. Le code compile, mais me donne un IndexOutOfBoundsException Index: 0 Taille: 0 aux lignes 70 et 28. Quelqu'un at-il des suggestions pour expliquer pourquoi cela se produit? Merci.IndexOutOfBoundsException Index 0 Taille 0

import java.util.*; 
import java.io.*; 

public class Lab8 
{ 
public static void main(String[] args) 
{ 
    if (args.length<1) { System.out.println("FATAL ERROR: Missing expression on command line\nexample: java Lab8 3+13/5-16*3\n"); System.exit(0); } 

    String expr= args[0]; // i.e. somethinig like "4+5-12/3.5-5.4*3.14"; 
    System.out.println("expr: " + expr); 
    ArrayList<String> operatorList = new ArrayList<String>(); 
    ArrayList<String> operandList = new ArrayList<String>(); 

    StringTokenizer st = new StringTokenizer(expr,"+-*/", true); 
    while (st.hasMoreTokens()) 
    { 
     String token = st.nextToken(); 
     if ("+-/*".contains(token)) 
      operatorList.add(token); 
     else 
      operandList.add(token); 
     } 
    System.out.println("Operators:" + operatorList); 
    System.out.println("Operands:" + operandList); 

    double result = evaluate(operatorList, operandList); 
    System.out.println("The expression: " + expr + " evalutes to " + result + "\n"); 
} // END MAIN 





static double evaluate(ArrayList<String> operatorList, ArrayList<String> operandList) 
{ 
    String operator; 
    double result; 

    ArrayList<Double> andList = new ArrayList<Double>(); 

    for(String op : operandList) 
    { 
     andList.add(Double.parseDouble(op)); 
    } 

    for(int i=0;i<operatorList.size();++i) 
    { 
     if(operatorList.get(i).equals("*")) 
     { 
     operator = operatorList.get(i); 
     } 
      result = andList.get(i) * andList.get(i+1); 
      andList.set(i,result); 
     //operandList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 


     if(operatorList.get(i).equals("/")) 
     { 
      operator = operatorList.get(i); 
     } 
      result = andList.get(i)/andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 



     if(operatorList.get(i).equals("+")) 
     { 
      operator = operatorList.get(i); 
     } 
      result = andList.get(i) + andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 



     if(operatorList.get(i).equals("-")) 
     { 
      operator = operatorList.get(i); 
     } 
      result = andList.get(i) - andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 

    } 

    return andList.get(0); 
} 

} //

+0

Quelles lignes sont 70 et 28? Merci de ne pas nous faire compter ... D'ailleurs, j'ai supprimé le tag "JavaScript" de votre question, car JavaScript et Java sont des langages complètement séparés. – nnnnnn

+0

'return andList.get (0);' cette liste est vide –

+0

Eh bien, quand je le compile et que je fournis manuellement 'expr' avec votre valeur d'exemple, j'obtiens la sortie suivante (aucune exception n'est levée): expr: 4 + 5-12/3,5-5,4 * 3,14 Opérateurs: [+, -, /, -, *] Opérandes: [4, 5, 12, 3.5, 5.4, 3.14] L'expression: 4 + 5-12/3.5-5.4 * 3.14 évalue à -0.2333333333333334 Processus terminé avec le code de sortie 0 – Coop

Répondre

-1

On dirait qu'il ya quelque chose de mal avec le code ci-dessous.

if(operatorList.get(i).equals("*")) 
      { 
       operator = operatorList.get(i); 
      } 
      result = andList.get(i) * andList.get(i+1); 
      andList.set(i,result); 
      //operandList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 


      if(operatorList.get(i).equals("/")) 
      { 
       operator = operatorList.get(i); 
      } 
      result = andList.get(i)/andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 



      if(operatorList.get(i).equals("+")) 
      { 
       operator = operatorList.get(i); 
      } 
      result = andList.get(i) + andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 



      if(operatorList.get(i).equals("-")) 
      { 
       operator = operatorList.get(i); 
      } 
      result = andList.get(i) - andList.get(i+1); 
      andList.set(i,result); 
      andList.remove(i+1); 
      operatorList.remove(i); 

Tous toutes les boucles si vous avez même ensemble de code

result = andList.get(i) * andList.get(i+1); 
       andList.set(i,result); 
       //operandList.set(i,result); 
       andList.remove(i+1); 
       operatorList.remove(i); 

qui devrait être probablement partie du précédent si le bloc. Ce qui se passe ici est lorsque le code atteint la ligne 70, il a déjà supprimé les éléments de operatorList dans les instructions operatorList.remove précédentes. Vous devez le corriger en vous assurant que le code appartient à if boucle .e.g. pour la première boucle if -

if(operatorList.get(i).equals("*")) 
    { 
     operator = operatorList.get(i); 
     result = andList.get(i) * andList.get(i+1); 
     andList.set(i,result); 
     //operandList.set(i,result); 
     andList.remove(i+1); 
     operatorList.remove(i); 
    } 

Est-ce la véritable exigence?