Hello everyone,

I am having trouble with my program its solving expressions and when I compile it I get a "Illegal start of expresson" line in my output. I am using Jgrasp as my softwear. May I ask to reveiw my code and see on what I did wrong? I am asking for learning purposes.

Thank You.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

       while(exp.contains(" ") || exp.contains("/"))
       {
       index1 = exp.indexOf("*");
       index2 = exp.indexOf("/");

       if(index1 <= 0)
       {
        index1 = 100;
       }
        if (index2 <= 0)
       {
        index2 = 100;
       }

        if(index1 < index2)
      {   
        num1 = Integer.parseInt(exp.get(index1-1));
        num2 = Integer.parseInt(exp.get(index1+1));
        answer = num1 * num2;

        exp.remove(index1 - 1);
        exp.remove(index1 - 1);
        exp.set(index1 - 1, Integer.toString(answer));
       }
        else if(index2 < index1)
       {
        num1 = Integer.parseInt(exp.get(index2 - 1));
        num2 = Integer.parseInt(exp.get(index2 + 1));
        answer = num1 / num2;

        exp.remove(index2 -1);
        exp.remove(index2 - 1);
        exp.set(index1 -1 ,Integer.toString(answer));

        while(exp.contains("+") || exp.contains("-"))
        {
           index1 = exp.indexOf("+");
           index2 = exp.indexOf("-");

           if(index1 <= 0)
           {
             index1 = 100;
           }
           if(index2 <= 0)
           {
            index2 = 100;
           }      
            if(index2 < index1)
        {   
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
         answer = num1 + num2;

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
        }
         else if(index2 < index1)
        {
         num1 = Integer.parseInt(exp.get(index2 - 1));
         num2 = Integer.parseInt(exp.get(index2 + 1));
         answer = num1 - num2;

         exp.remove(index2 - 1);
         exp.remove(index2 - 1);
         exp.set(index1 -1 ,Integer.toString(answer));
         }

   }
}

  public String toString()   
  {
   return expString + " = " + answer;
  }
}

// My runner class

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {
        ExpressionSolver test = new ExpressionSolver("15 / 3 + 2");
        test.solveExpression();
       System.out.println(test);
    }
}

//My output message

 ----jGRASP exec: javac -g ExpressionSolver.java
ExpressionSolver.java:108: error: illegal start of expression
  public String toString()   
  ^
ExpressionSolver.java:108: error: ';' expected
  public String toString()   
                        ^
ExpressionSolver.java:112: error: reached end of file while parsing
}
 ^
3 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Reverend Jim commented: Asked nicely, picked a good title, showed proof of effort. +15

Recommended Answers

All 25 Replies

Please share what line number is being reported as having the error. You might have to enable more features on the compiler if it doesn't do this yet. Or if I read this as line 108, that doesn't jive with your code as posted.

Note: Found the missing tostring line.

99% certain it’s an error in your brackets.
Line 79 is formatted as if it should be a close bracket, but it’s an open bracket. Suspicious!

commented: That's likely it. Unbalanced brackets. For each left there shall be a right. +0

I think 79-97 are all part of the same if-then-else-end if construct, just badly indented.

Hello everyone,

Thank you for your tips, I have recheck my brackets and I was missling 2 of them and now it works.

Again thank you for your help.

Abdullah

EDIT: The complier works but whne I run the program it prints out the number but the answer is 0

Here is my updated code

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

       while(exp.contains(" ") || exp.contains("/"))
       {
       index1 = exp.indexOf("*");
       index2 = exp.indexOf("/");

       if(index1 <= 0)
       {
        index1 = 100;
       }
        if (index2 <= 0)
       {
        index2 = 100;
       }

        if(index1 < index2)
      {   
        num1 = Integer.parseInt(exp.get(index1-1));
        num2 = Integer.parseInt(exp.get(index1+1));
        answer = num1 * num2;

        exp.remove(index1 - 1);
        exp.remove(index1 - 1);
        exp.set(index1 - 1, Integer.toString(answer));
       }
        else if(index2 < index1)
       {
        num1 = Integer.parseInt(exp.get(index2 - 1));
        num2 = Integer.parseInt(exp.get(index2 + 1));
        answer = num1 / num2;

        exp.remove(index2 -1);
        exp.remove(index2 - 1);
        exp.set(index1 -1 ,Integer.toString(answer));
        }
      }
        while(exp.contains("+") || exp.contains("-"))
        {
           index1 = exp.indexOf("+");
           index2 = exp.indexOf("-");

           if(index1 <= 0)
           {
             index1 = 100;
           }
           if(index2 <= 0)
           {
            index2 = 100;
           }      
            if(index2 < index1)
        {   
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
         answer = num1 + num2;

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
        }
         else if(index2 < index1)
        {
         num1 = Integer.parseInt(exp.get(index2 - 1));
         num2 = Integer.parseInt(exp.get(index2 + 1));
         answer = num1 - num2;

         exp.remove(index2 - 1);
         exp.remove(index2 - 1);
         exp.set(index1 -1 ,Integer.toString(answer));
         }

   }
}

  public String toString()   
  {
   return expString + " = " + answer;
  }
}

My runner class

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {  
      ExpressionSolver test = new ExpressionSolver(" 5 + 3");
      test.solveExpression();
      System.out.println(test);

    }  

}

My output is

5 + 3 = 0 , it does not equal to 8.

Matching up { and } can be a pain. If you are running under Windows you can use the following script to reformat or do a quick check. Save the code into redent.vbs and run it from the command line by

 cscript redent.vbs myfile.java

or, if you have previously ever done

cscript //h:cscript //nologo //s

then you can run it by

redent myfile.jave

Here is redent.vbs

' Read the java file given on the command line and re-indent it based
' on the presence of '{' and '}' characters. Report if unmatched. Output
' is to the console.

Set fso = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Unnamed.Count = 0 Then
    WScript.Echo "redent <filename>"
    WScript.Quit 
End If

file = WScript.Arguments.Unnamed(0)

If Not fso.FileExists(file) Then
    WScript.Echo "File '" & file & "' not found"
    WScript.Quit 
End If

code = fso.OpenTextFile(file).ReadAll
code = Replace(code,vbTab,"    ")
code = Replace(code,vbCr,"")
code = Split(code,vbLf)

indent = 0

For Each line In code

    line = Trim(line)

    Select Case True
        Case InStr(line,"{") > 0
            Output line, indent
            indent = indent + 4
        Case InStr(line,"}") > 0
            indent = indent - 4
            Output line, indent
        Case Else
            Output line, indent
    End Select

Next

If indent <> 0 Then
    WScript.Echo "Error: You have",indent/4,"unmatched '{'"
End If    

Sub Output (line,indent)

    If indent < 0 Then
        WScript.Echo "Error: '}' without matching '{'"
        WScript.Quit 
    End If

    WScript.Echo Space(indent) & line

End Sub

I've only take a look to addition and substraction and made some modifications. I hope you understand it and leave up to you the other operators.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

       while(exp.contains("*") || exp.contains("/"))
       {
         index1 = exp.indexOf("*");
         index2 = exp.indexOf("/");

         if(index1 <= 0)
         {
          index1 = 100;
         }
         if (index2 <= 0)
         {
          index2 = 100;
         }

          if(index1 < index2)
         {   
          num1 = Integer.parseInt(exp.get(index1-1));
          num2 = Integer.parseInt(exp.get(index1+1));
          answer = num1 * num2;

          exp.remove(index1 - 1);
          exp.remove(index1 - 1);
          exp.set(index1 - 1, Integer.toString(answer));
         }
         else if(index2 < index1)
         {
           num1 = Integer.parseInt(exp.get(index2 - 1));
           num2 = Integer.parseInt(exp.get(index2 + 1));
           answer = num1 / num2;

           exp.remove(index2 -1);
           exp.remove(index2 - 1);
           exp.set(index1 -1 ,Integer.toString(answer));
          }
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));

      }
}

  public String toString()   
  {
   return expString + " = " + answer;
  }
}

Thank You guys again for all of your help. I have learned on what I did wrong.

Hey guys,

I am sorry for everything. As @xrj said that he modified the "+" and "-" operation method on the code below and I am stumoed on the "*" and "/" operation. May I ask on what I need to do.

Again I am sorry for getting this help. I never ask for alot of help.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
       while(exp.contains("*") || exp.contains("/"))
       {
         index1 = exp.indexOf("*");
         index2 = exp.indexOf("/");
         if(index1 <= 0)
         {
          index1 = 100;
         }
         if (index2 <= 0)
         {
          index2 = 100;
         }
          if(index1 < index2)
         {   
          num1 = Integer.parseInt(exp.get(index1-1));
          num2 = Integer.parseInt(exp.get(index1+1));
          answer = num1 * num2;
          exp.remove(index1 - 1);
          exp.remove(index1 - 1);
          exp.set(index1 - 1, Integer.toString(answer));
         }
         else if(index2 < index1)
         {
           num1 = Integer.parseInt(exp.get(index2 - 1));
           num2 = Integer.parseInt(exp.get(index2 + 1));
           answer = num1 / num2;
           exp.remove(index2 -1);
           exp.remove(index2 - 1);
           exp.set(index1 -1 ,Integer.toString(answer));
          }
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
}
  public String toString()   
  {
   return expString + " = " + answer;
  }
}

You just have to proceed similarly as for the "+" and "-" operators. Now, the code is:

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

      while(exp.contains("*") || exp.contains("/"))
      {
           index1 = exp.indexOf("*");
           index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("/");
             index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 * num2;
         }
         else
         {
            answer = num1 / num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));

      }
}

  public String toString()   
  {
   return expString + " = " + answer;
  }
}

@xrj I have changed the code in my program and when I run it it says "Out of bounce exeception: Index 99, size 1"

//Main

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
      while(exp.contains("*") || exp.contains("/"))
      {
           index1 = exp.indexOf("*");
           index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("/");
             index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 * num2;
         }
         else
         {
            answer = num1 / num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
}
  public String toString()   
  {
   return expString + " = " + answer;
  }
}

//Runner

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {  
      ExpressionSolver test = new ExpressionSolver("3 + 5");
      test.solveExpression();
      System.out.println(test);

      test.setExpression("3 / 3");
      test.solveExpression();
      System.out.println(test);

      test.setExpression("5 / 5 * 2 + 8 / 2 + 5");
      test.solveExpression();
      System.out.println(test);

      test.setExpression("5 * 5 + 2 / 2 - 8 + 5 * 5 – 2");
      test.solveExpression();
      System.out.println(test);
    }  

}

//output

3 + 5 = 8

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 99, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.set(ArrayList.java:444)
    at ExpressionSolver.solveExpression(ExpressionSolver.java:60)
    at ExpressionSolverRunner.main(ExpressionSolverRunner.java:22)

It comes that error at 3 / 3

If you set the code as I send it to you, you won't have that problem with none of those examples.

Hi XRJ
Are you sure you’re teaching OP rather than just giving code he can copy/paste imperfectly?
The index of 99 suggests to me that he’s still running the original code where he set the index values to 100

commented: Bending the curve at school. Do everyone's homework, you move up the ranks on tests. They learned nothing. +15

You are right @JamesCherill, that's the same feeling I'm getting: copy and paste.

Guys,

I am sorry that I am asking for so much help. I have learned on what you wrote I am not here just copying and pasting, I am here to learn. I know you guys probably hate me alot since you think that I am here just for answers while I am not I am like alot of other people learning and helping.

Again I am sorry and I wont be beg for answers, I just want to learn.

commented: I sense no hate here. +15

It's worth noting the the OP did the following:

  1. asked politely
  2. chose a descriptive thread title
  3. posted details
  4. posted proof of effort

That's a lot more than we get from most of the new posters.

Abdullah;
Hey no, I was not criticising you. I was suggesting to xrj that maybe he could have explained what the problem was, rather than just posting fixed code. You have done absolutely nothing wrong.

JamesCherrill,

Thanks for the comment it makes sense now. I acctually hate it when people just give me the answers. I want to know on what I did wrong with my code so I can lean and do a program similar to it later on.

Hello,

May I ask on what is wrong with my division operation. When the program reads it it comes out as an error.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
      while(exp.contains("*") || exp.contains("/"))
      {
           index1 = exp.indexOf("*");
           index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("/");
             index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 * num2;
         }
         else
         {
            answer = num1 / num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
}
  public String toString()   
  {
   return expString + " = " + answer;
  }
}
//Runner
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {  
      ExpressionSolver test = new ExpressionSolver("3 + 5");
      test.solveExpression();
      System.out.println(test);
      test.setExpression("3 / 3");
      test.solveExpression();
      System.out.println(test);
      test.setExpression("5 / 5 * 2 + 8 / 2 + 5");
      test.solveExpression();
      System.out.println(test);
      test.setExpression("5 * 5 + 2 / 2 - 8 + 5 * 5 – 2");
      test.solveExpression();
      System.out.println(test);
    }  
}
//output
3 + 5 = 8
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 99, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.set(ArrayList.java:444)
    at ExpressionSolver.solveExpression(ExpressionSolver.java:60)
    at ExpressionSolverRunner.main(ExpressionSolverRunner.java:22)

The code is throwing no error. Seems as if you haven't compiled the new code. You should execute "javac" from the command prompt, have you?

@xrj, I have re-typed all of my code and it worked as expected. When the program reads 3 / 3 it prints out 0 but, when the program reads "5 / 5 * 2 + 8 / 2 + 5" it prints iut 17 and not 11.

It has to do with the algorithm. The usual method for parsing mathematical expressions is the shunting yard algorithm. You may consider reading it's description here.

3/3 should be 1, not 0

commented: @JamesCherrill, I made a typo it suppsoe to say 3 / 5 not 3 / 3. my bad +0

Guys here is my output for the program

3 + 3 = 6
3 * 5 = 15
3 - 5 = -2
3 / 3 = 1
5 / 5  *  2  +  8  +  8  /  2  +  5 = 17 (not suppsoe to be 17 it suppose to be 11 calculate it)

My output should look like this:

3 + 5 = 8
3 * 5 = 15
3 - 5 = -2
3 / 5 = 0
5  /  5  *  2  +  8  /  2  +  5  = 11
5 * 5 + 2 / 2 - 8 + 5 * 5 - 2 = 41

Just a note - Markdown syntax uses * (and a few others) as formatting characters. In order to use these chars for actual display you either have to

  1. "escape" them by preceding with a back slash
  2. indenting the line so it gets displayed literally (as code)
  3. enclosing them in back-tics to display as inline code

Click on ? in the text window toolbar for more complete Markdown formatting help.

You evaluate all the multiplications before you evaluate any divisions. That’s wrong, you should evaluate them all left to right.
In normal arithmetic this doesn’t matter, but with integer arithmetic it is important
Eg

        5/5*2

Should do the divide first, giving 1, then multiply by 2, result 2
If you do the multiply first you divide 5 by 10, result 0

Ps you seem to have +8 twice

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.