Java:Evaluation of Mathematical Expression only from left to right only.
I have written a Java program which evaluates a mathematical expression from left to right only.however im stuck up with the output.Im not getting the desired output.
Please Note: There is no precedence in operators(evaluation should be from LEFT->RIGHT.)
Code :
/*
* Created on Jul 10, 2011
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.comcast;
import java.util.*;
public class Evaluation
{
private static final char[] validOperators = {'/','*','+','-'};
private Evaluation()
{
/* Using a private contructor to prevent instantiation
Using class as a simple static utility class
*/
}
private static int evaluate(String leftSide, char oper, String rightSide)
throws IllegalArgumentException
{
System.out.println("Evaluating: " + leftSide + " (" + oper + ") " + rightSide);
int total = 0;
int leftResult = 0;
int rightResult = 0;
String originalString =leftSide;
int operatorLoc = findOperatorLocation(leftSide);
leftSide = leftSide.substring(0,operatorLoc);
rightSide = originalString.substring(operatorLoc+1,operatorLoc+2);
String remainingString = originalString.substring(operatorLoc+2,originalString.length());
System.out.println("leftSide -->"+leftSide);
System.out.println("rightSide -->"+rightSide);
System.out.println("remainingString --->"+remainingString);
try{
leftResult = Integer.parseInt(leftSide);
}catch(Exception e){
throw new IllegalArgumentException("Invalid value found in portion of equation: "
+ leftSide);
}
try{
rightResult = Integer.parseInt(rightSide);
}catch(Exception e){
throw new IllegalArgumentException("Invalid value found in portion of equation: "
+ rightSide);
}
System.out.println("Getting result of: " + leftResult + " " + oper + " " + rightResult);
switch(oper)
{
case '/':
total = leftResult / rightResult; break;
case '*':
total = leftResult * rightResult; break;
case '+':
total = leftResult + rightResult; break;
case '-':
total = leftResult - rightResult; break;
default:
throw new IllegalArgumentException("Unknown operator.");
}
System.out.println("Returning a result of: " + total);
String totally = String.valueOf(total)+remainingString;
return evaluate(totally,findCharacter(totally),remainingString);
}
private static int findOperatorLocation(String string)
{
int index = -1;
index = string.indexOf(string.substring(1,2));
if(index >= 0){
return index;
}
return index;
}
private static char findCharacter(String string){
char c='\u0000';
int index = -1;
index = string.indexOf(string.substring(1,2));
if(index >= 0){
c = string.charAt(index);
return c;
}
return c;
}
public static int processEquation(String equation)
throws IllegalArgumentException
{
return evaluate(equation,'+',"0");
}
public static void main(String[] args)
{
//String usage = "Usage: java MathParser equation\nWhere equation is a series"
// + " of integers separated by valid operators (+,-,/,*)";
//if(args.length < 1 || args[0].length() == 0)
// System.out.println(usage);
Scanner input = new Scanner(System.in);
System.out.print("Enter the equation to be evaluated ");
String equation = (String)input.next();
int result = Evaluation.processEquation(equation);
System.out.println("The result of your equation ("
+ equation + ") is: " + result);
//catch(IllegalArgumentException iae)
//{
// System.out.println(iae.getMessage() + "\n" + usage);
//}
}
}
Expression Input:3+5*2-5 =>3+5*2-5 =>8*2-5 =>16-5 =>Expected Output :11
im getting as below output Enter the equation to be evaluated 3+5*2-5
Evaluating: 3+5*2-5 (+) 0
leftSide -->3
rightSide -->5
remainingString --->*2-5
Getting result of: 3 + 5
Returning a result of: 8
Evaluating: 8*2-5 ( * ) *2-5
leftSide -->8
rightSide -->2
remainingString --->-5
Getting result of: 8 * 2
Returning a result of: 16
Evaluating: 16-5 (6) -5
leftSide -->1
rightSide -->-
remainingString --->5
Exception in thread "main" java.lang.IllegalArgumentException: Invalid value found in portion of equation: -
at Evaluation.evaluate(Evaluation.java:49)
at Evaluation.evaluate(Evaluation.java:70)
at Evaluation.evaluate(Evaluation.java:70)
at Evaluation.processEquation(Evaluation.java:98)
at Evaluation.main(Evaluation.java:112)
Im unable to make my program generic for any equation entered.Help provided will be appreciated.Please note this is not a homework question.
Need help !!!!!Code fix required for the above program
Re: Java:Evaluation of Mathematical Expression only from left to right only.
You must have a logic error or a bad expression.
Have you tried more debugging aids by adding more printlns to show how the variables change?
Re: Java:Evaluation of Mathematical Expression only from left to right only.
The problem is in the method operatorLoc where im hardcoding the subString(1,2) to find location of operator,when i encounter 16-5,it returns a wrong location for operator.which is (6) instead of (-).
Need help in resolving this ? Please provide the bug fix for the same.
Re: Java:Evaluation of Mathematical Expression only from left to right only.
Quote:
operatorLoc where im hardcoding the subString(1,2) to find location of operator,when i encounter 16-5,it returns a wrong location for operator.which is (6) instead of (-).
Can you explain what this code is supposed to do:
Code :
index = string.indexOf(string.substring(1,2));
Also what different values will it return?
Re: Java:Evaluation of Mathematical Expression only from left to right only.
Quote:
Originally Posted by
deepakl_2000
The problem is in the method operatorLoc where im hardcoding the subString(1,2)
So don't do that. You can either split your String up so that 2 or more digit numbers stay as a whole number or parse your equation by reading until you reach an operator. Everything upto that point must be the operand.
Re: Java:Evaluation of Mathematical Expression only from left to right only.
Hi Junky,Can you provide the code fix for my code.??@-)
@-)
Re: Java:Evaluation of Mathematical Expression only from left to right only.
No. It is your job to write it not mine.
Re: Java:Evaluation of Mathematical Expression only from left to right only.
Look at post#4 again. Can you describe what that code is supposed to do?
The way it is coded, it will always set index to 1. Unless the string is too short.