Java:Evaluation of Mathematical Expression only from left to right -
Java:Evaluation of Mathematical Expression only from left to right -
i have written java programme evaluates mathematical look left right (no precedence, left right). however, i'm not getting desired output.
import java.util.*; public class evaluation { //private static final char[] validoperators = {'/','*','+','-'}; private evaluation() { /* using private contructor prevent instantiation using class 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); seek { leftresult = integer.parseint(leftside); } catch(exception e) { throw new illegalargumentexception( "invalid value found in portion of equation: " + leftside); } seek { 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 result of: " + total); string totally = string.valueof(total)+remainingstring; homecoming evaluate(totally,findcharacter(totally),remainingstring); } private static int findoperatorlocation(string string) { int index = -1; index = string.indexof(string.substring(1,2)); if(index >= 0) { homecoming index; } homecoming 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); homecoming c; } homecoming c; } public static int processequation(string equation) throws illegalargumentexception { homecoming evaluate(equation,'+',"0"); } public static void main(string[] args) { //string usage = "usage: java mathparser equation\nwhere equation series" // + " of integers separated valid operators (+,-,/,*)"; //if(args.length < 1 || args[0].length() == 0) // system.out.println(usage); scanner input = new scanner(system.in); system.out.print("enter equation evaluated "); string equation = (string)input.next(); int result = evaluation.processequation(equation); system.out.println("the result of equation (" + equation + ") is: " + result); //catch(illegalargumentexception iae) //{ // system.out.println(iae.getmessage() + "\n" + usage); //} } } here input i'm trying use, , expect:
3+5*2-5 =>8*2-5 =>16-5 =>expected output :11
but i'm getting output:
enter equation evaluated 3+5*2-5 evaluating: 3+5*2-5 (+) 0 leftside -->3 rightside -->5 remainingstring --->*2-5 getting result of: 3 + 5 returning result of: 8 evaluating: 8*2-5 (*) *2-5 leftside -->8 rightside -->2 remainingstring --->-5 getting result of: 8 * 2 returning 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: - @ evaluation.evaluate(evaluation.java:49) @ evaluation.evaluate(evaluation.java:70) @ evaluation.evaluate(evaluation.java:70) @ evaluation.processequation(evaluation.java:98) @ evaluation.main(evaluation.java:112)
i'm unable create programme generic equation entered. appreciate help can provide. please note not homework question.
you getting error because rightside has string value "-" , attempting integer.parseint() on it.
java
Comments
Post a Comment