Can some one please help me with this ?

import java.util.*;


public class Expression{
    /*
    * Strings used for storing expression.
    */
    String s, x;

    /*
    * Term evaluator for number literals.
    */
    double term(){
        double ans = 0;
        StringBuffer temp = new StringBuffer();
        while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
            temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
            s = s.substring( 1 );
        }
        if( s.length() > 0 && s.charAt( 0 ) == '.' ){
            temp.append( '.' );
            s = s.substring( 1 );
            while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
                temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
                s = s.substring( 1 );
            }
        }
        if( s.length() > 0 && (s.charAt(0) == 'e' || s.charAt(0) == 'E') ){
            temp.append( 'e' );
            s = s.substring( 1 );
            temp.append( s.charAt( 0 ) );
            s = s.substring( 1 );
            while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
                temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
                s = s.substring( 1 );
            }
        }
        ans = Double.valueOf( temp.toString() ).doubleValue();
        return ans;
    }

    /*
    * Parentheses solver.
    */
    double paren(){
        double ans;
        if( s.charAt( 0 ) == '(' ){
            s = s.substring( 1 );
            ans = add();
            s = s.substring( 1 );   // we assume this is a ')'
        } else {
            ans = term();
        }
        return ans;
    }

    /*
    * Exponentiation solver.
    */
    double exp(){
        boolean neg = false;
        if( s.charAt( 0 ) == '-' ){
            neg = true;
            s = s.substring( 1 );
        }
        double ans = term();
        while( s.length() > 0 ){
            if( s.charAt( 0 ) == '^' ){
                s = s.substring( 1 );
                boolean expNeg = false;
                if( s.charAt( 0 ) == '-' ){
                    expNeg = true;
                    s = s.substring( 1 );
                }
                double e = term();
                if( ans < 0 ){       // if it's negative
                    double x = 1;
                    if( Math.ceil(e) == e ){    // only raise to an integer
                        if( expNeg )
                            e *= -1;
                        if( e == 0 )
                            ans = 1;
                        else if( e > 0 )
                            for( int i = 0; i < e; i++ )
                                x *= ans;
                        else
                            for( int i = 0; i < -e; i++ )
                                x /= ans;
                        ans = x;
                    } else {
                        ans = Math.log(-1); // otherwise make it NaN
                    }
                }
                else if( expNeg )
                    ans = Math.exp( -e*Math.log( ans ) );
                else
                    ans = Math.exp( e*Math.log( ans ) );
            } else
                break;
        }
        if( neg )
            ans *= -1;
        return ans;
    }


    /*
    * Trigonometric function solver.
    */
    double trig(){
        double ans = 0;
        boolean found = false;
        if( s.indexOf( "sin" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.sin( trig() );
            found = true;
        } else if( s.indexOf( "cos" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.cos( trig() );
            found = true;
        } else if( s.indexOf( "tan" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.tan( trig() );
            found = true;
        }
        if( !found ){
            ans = term();
        }
        return ans;
    }

    /*
    * Multiplication, division expression solver.
    */
    double mul(){
        double ans = term();
        while( s.length() > 0 ){
            if( s.charAt( 0 ) == '*' ){
                s = s.substring( 1 );
                ans *= term();
            } else if( s.charAt( 0 ) == '/' ){
                s = s.substring( 1 );
                ans /= term();
            } else break;
        }
        return ans;
    }

    /*
    * Addition, subtraction expression solver.
    */
    double add(){
        double ans = term();
        while( s.length() > 0 ){
            if( s.charAt( 0 ) == '+' ){
                s = s.substring( 1 );
                ans += term();
            } else if( s.charAt( 0 ) == '-' ){
                s = s.substring( 1 );
                ans -= term();
            } else{
                break;
            }
        }
        return ans;
    }

    /*
    * Public access method to evaluate this expression.
    */
        public double evaluate(){
        s = x.intern();
        return term();
                }

    /*
    * Creates new Expression.
    */

public Expression( String s ){
        // remove white space, assume only spaces or tabs
        StringBuffer b = new StringBuffer();
        StringTokenizer t = new StringTokenizer( s, " " );
        while( t.hasMoreElements() )
            b.append( t.nextToken() );
        t = new StringTokenizer( b.toString(), "\t" );
        b = new StringBuffer();
        while( t.hasMoreElements() )
            b.append( t.nextToken() );

        x = b.toString();
    }

    /*
    * The String value of this Expression.
    */
    public String toString(){
        return x.intern();
    }

    /*
    * Test our Expression class by evaluating the command-line
    * argument and then returning.
    */

        public static void main( String[] args ){

        Expression e = new Expression( (args[0]) );

//error on the above line ?

        System.out.println( e + " = " + e.evaluate() );
    }


}

Expression e = new Expression( (args[0]) );

error is on that line in the public static void main

Please post the full text of the error message that shows what the index's value is.

If an array is empty, it does not have a first element (index of 0)
The code should test the array's length BEFORE trying to index into it.
A length of 0 means there are no elements.

thank you for answering ,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at expression.ExpressionApplet.main(ExpressionApplet.java:41)
Java Result: 1

that is the error message i get

Did you undererstand my explanation of the problem and how to fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.