Shorter solution if, else if, else if

I am looking for a way to shorten this code and avoid repeating code and if statements. What I am doing is creating a calculator that searches for strings for the "* / + -" operators and executes them accordingly. Any ideas?

if(exp.charAt(i)=='*') { newResult=Integer.parseInt(exp.substring(0, i)) * Integer.parseInt(exp.substring(i+1, exp.length())); primeResult = newResult; System.out.println(primeResult); } else if(exp.charAt(i)=='/') { newResult=Integer.parseInt(exp.substring(0, i)) / Integer.parseInt(exp.substring(i+1, exp.length())); primeResult = newResult; System.out.println(primeResult); } else if(exp.charAt(i)=='+') { newResult=Integer.parseInt(exp.substring(0, i)) + Integer.parseInt(exp.substring(i+1, exp.length())); primeResult = newResult; System.out.println(primeResult); } else if(exp.charAt(i)=='-') { newResult=Integer.parseInt(exp.substring(0, i)) - Integer.parseInt(exp.substring(i+1, exp.length())); primeResult = newResult; System.out.println(primeResult); } 

Also, is there a decision to accept a string with more than two operands? those. 5 + 10 * 2/3

+4
source share
6 answers

To change the code, you can use the switch statement and place part of the redundant code before or after the switch.

 int left = Integer.parseInt(exp.substring(0,i)); int right = Integer.parseInt(exp.substring(i+1,exp.length())); switch(exp.charAt(i)){ case '*': primeResult = left * right; break; case '/': ... break; case '+': ... break; case '-': ... break; default: ... // Error Handling. } System.out.println(primeResult); 
+10
source

No switch or complex hierrachies classes are needed.

To simplify and shorten the code and calculate simple and complex expressions (represented as String objects), you can use the Java JavaScript API class and ScriptEngine , which basically mimics the JavaScript console.

 import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; public class MyClass{ public static void main(String[] args) throws Exception { // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a JavaScript engine ScriptEngine engine = factory.getEngineByName("JavaScript"); // evaluate JavaScript code from String System.out.println(engine.eval("(5+10)*2/3")); } } 

This will output: 10.0

+6
source

You can write the AbstractCalculationOperation class using the execute method, with Add , Subtract , etc., extending it.

Then just leftHand , rightHand and calculationOperation and run calculationOperation.execute( rightHand, leftHand ) .

  public interface CalculationOperation { double calculate ( double lh, double rh ); long calculate ( long lh, long rh ); } public class Add implements CalculationOperation { public static final CalculationOperation INSTANCE = new Add(); public double calculate ( double rh, double lh ) { return lh + rh; } public long calculate ( long rh, long lh ) { return lh + rh; } } 

And then:

  int lh = exp.substring(0, i); int rh = exp.substring(i+1); CalculationOperation op; switch( exp.charAt(i) ) { case '*': op = Multiply.INSTANCE; break; case '/': op = Divide.INSTANCE; break; case '+': op = Add.INSTANCE; break; case '-': op = Subtract.INSTANCE; break; } newResult = op.calculate( rh, lh ); primeResult = newResult; System.out.println(primeResult); 

Alternative listing option:

  public enum Calculation { ADD('+') { public int calculate( int lhs, int rhs ) { return lhs + rhs; } public long calculate( long lhs, long rhs ) { return lhs + rhs; } public float calculate( float lhs, float rhs ) { return lhs + rhs; } public double calculate( double lhs, double rhs ) { return lhs + rhs; } }, SUBTRACT('-') { public int calculate( int lhs, int rhs ) { return lhs - rhs; } public long calculate( long lhs, long rhs ) { return lhs - rhs; } public float calculate( float lhs, float rhs ) { return lhs - rhs; } public double calculate( double lhs, double rhs ) { return lhs - rhs; } }, MULTIPLY('*') { public int calculate( int lhs, int rhs ) { return lhs * rhs; } public long calculate( long lhs, long rhs ) { return lhs * rhs; } public float calculate( float lhs, float rhs ) { return lhs * rhs; } public double calculate( double lhs, double rhs ) { return lhs * rhs; } }, DIVIDE('/') { public int calculate( int lhs, int rhs ) { return lhs / rhs; } public long calculate( long lhs, long rhs ) { return lhs / rhs; } public float calculate( float lhs, float rhs ) { return lhs / rhs; } public double calculate( double lhs, double rhs ) { return lhs / rhs; } }; private final char textValue; Calculation ( char textValue ) { this.textValue = textValue; } public abstract int calculate ( int lht, int rhs ); public abstract long calculate ( long lht, long rhs ); public abstract float calculate ( float lht, float rhs ); public abstract double calculate ( double lht, double rhs ); public static Calculation fromTextValue( char textValue ) { for( Calculation op : values() ) if( op.textValue == textValue ) return op; throw new IllegalArgumentException( "Unknown operation: " + textValue ); } } 

and then:

  int lh = exp.substring(0, i); int rh = exp.substring(i+1); Calculation op = Calculation.fromTextValue( exp.substring(i,1) ); newResult = op.calculate( lh, rh ); primeResult = newResult; System.out.println(primeResult); 
+4
source

Reduce the code by capturing variables separately from the operation. This will not reduce your "if" jobs, but it will drastically reduce line numbers.

Do not make several variables until you understand the trees ... I have never worked with them personally, but I think that expression trees are what you need. (note: I just checked on google, yep, Expression Trees)

0
source

This is pretty simple, how to avoid too much code repetition:

 Integer op1= Integer.parseInt(exp.substring(0, i); Integer op2=Integer.parseInt(exp.substring(i+1, exp.length())); if(exp.charAt(i)=='*') { newResult=op1 * op2; } else .... primeResult = newResult; System.out.println(primeResult); 

But to do something more general, reliable and useful, with arbitrary levels of nesting, you must use some real parser. For instance.

0
source

Here is a snippet:

 public static void main(String[] args) { float primeResult; String exp = "4-2"; int i = 1; ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); char[] myVar = new char[] { '*', '/', '-', '+' }; for (int myVarCtr = 0; myVarCtr < myVar.length; myVarCtr++) { if (exp.charAt(i) == myVar[myVarCtr]) { try { primeResult = Float.parseFloat(engine.eval( (Integer.parseInt(exp.substring(0, i))) + Character.toString(myVar[myVarCtr]) + (Integer.parseInt(exp.substring(i + 1, exp.length())))).toString()); System.out.println(primeResult); } catch (ScriptException e) { e.printStackTrace(); } } } } 
0
source

Source: https://habr.com/ru/post/1482288/


All Articles