-3

I am writing a program that pretty much is taking a string of numbers and operators, splitting them up, then doing the calculations on it. I have four separate IF statements for each operator inside a For loop. The code compiles, but gives me an IndexOutOfBoundsException Index:0 Size:0 at lines 70 and 28. Does anyone have any suggestions as to why this is happening? Thanks.

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

} //

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Ironman131
  • 13
  • 2
  • Which lines are 70 and 28? Please don't make us count for ourselves... By the way, I've removed the "JavaScript" tag from your question, because JavaScript and Java are completely separate languages. – nnnnnn Mar 29 '17 at 02:40
  • `return andList.get(0);` this list is empty – Scary Wombat Mar 29 '17 at 02:41
  • Well when I compile it and manually supply `expr` with your example value I get the following output (no exceptions thrown) : expr: 4+5-12/3.5-5.4*3.14 Operators:[+, -, /, -, *] Operands:[4, 5, 12, 3.5, 5.4, 3.14] The expression: 4+5-12/3.5-5.4*3.14 evalutes to -0.2333333333333334 Process finished with exit code 0 – Coop Mar 29 '17 at 02:44
  • The four lines after each `if` block should be *inside* those `if` blocks, shouldn't they? They're indented as if you think they belong to the `if`s, but they're after the closing `}` of each block. Currently the only thing *inside* each `if` block is an assignment to a variable that is never read. – nnnnnn Mar 29 '17 at 02:44

1 Answers1

-1

Looks like there is something wrong with below code.

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

All all the if loops you have same set of code

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

which should be probably part of the previous if block. What is happening here is when the code reach to line 70, it has already removed the elements from operatorList in previous operatorList.remove statements. You need to correct it by making sure if the code belongs to if loop .e.g. for first if loop -

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

Is that the real requirement?

vlaxmi
  • 468
  • 4
  • 18