MrJazsohanisharma

Java Expression Solver with File Input – Handle +, -, *, / with Operator Precedence

Comments

 Learn how to build a Java program that reads expressions like 3*5, 5/5*2+8/2+5 from a text file and solves them using correct operator precedence. This project involves file I/O, string parsing, handling * and / before + and -, and ensuring left-to-right evaluation for multiplication/division. Full working code for both the ExpressionSolver and the ExpressionSolverRunner is included.

Java Expression Solver with File Input – Handle +, -, *, / with Operator Precedence


The programmer will write the  ExpressionSolverRunner that will use the ExpressionSolver class to solve  an integer expression provided by the ExpressionSolver textfile. An  expression will a String and it will be comprised of number and the  mathematical operators +, *, /, and -. The % operation will not be a  part of this program. In order to read an expression, the user must read  the expression from left to right performing the multiplication and  division operators before attempting the subtraction arguments. The  programmer should be careful to follow the order of operations, or they  will not get the correct answer. The calculation is integer mathematics,  so there will be no rounding but just truncation. The programmer should  take particular care when a multiplication and a division or next to  each other and make sure to do the operations from left to right. There  will be no parenthesis in the expression. 

 

Answer

 

 

import java.util.*;

import java.io.*;

 

public class ExpressionSolverRunner {

 

public static void main( String args[] ){

 

String s1 = "Java isn't just for breakfast.";

 

String s2 = "JAVA isn't just for breakfast.";

 

if (s1.equals(s2))

 

System.out.println("The two lines are equal.");

 

else

 

System.out.println("The two lines are not equal.");

 

if (s1.equalsIgnoreCase(s2))

 

System.out.println("But the lines are equal, ignoring case.");

 

else

 

System.out.println("Lines are not equal, even ignoring case.");

 

 

Scanner input =new Scanner(System.in);

 

System.out.print ("Please enter a file name to read :");

String filename = input.nextLine();

 

// // instance object

ExpressionSolver problem = new ExpressionSolver();

 

// read file as  3*5 3-5 5/5*2+8/2+5

try {

 

  File myObj = new File(filename);

 

  Scanner myReader = new Scanner(myObj);

 

 while (myReader.hasNextLine()) {

 

                String data = myReader.nextLine();

                String words[] = data.split(" ");

 

                for(int i = 0; i < words.length ; i++){

 

                 System.out.print (problem.CalculationExpression(words[i]));

 

                                }

 

 

  } // end while

 

  myReader.close();

 

} catch (FileNotFoundException e) {

  System.out.println("An error occurred.");

 

}

 

 

 

}// end main

 

}// end class

 

 

 

 

import java.util.*;

 

 

 public class ExpressionSolver

{

 

public static String  CalculationExpression (String ExpressionToCalculate) {

 

 

String expression  = ExpressionToCalculate, StringCalculation = "" , provisional;

int PositionA = 0 , indexJ , indexK,postive,negative;

Integer SignA , SignB , val;

 

for (;ExpressionToCalculate.contains("*") || ExpressionToCalculate.contains("/");) {

indexK = ExpressionToCalculate.indexOf("*");

indexJ = ExpressionToCalculate.indexOf("/");

 

if (indexK == -1) {

 

                StringCalculation = "/";

                }

else if (indexJ  > indexK) {

 

                StringCalculation = "*";

                }

else if (indexJ == -1) {

 

                StringCalculation = "*";

                }

else if (indexK  > indexJ) {

                StringCalculation = "/";

                }

 

                PositionA = ExpressionToCalculate.indexOf(StringCalculation);

                SignA = SignB = 0;

 

                int i = PositionA;

 

for (  ;  i > 0  ; ) {

 

                try {

                i--;

                SignA = Integer.parseInt(ExpressionToCalculate.substring(i, PositionA));

 

                } catch (NumberFormatException exception ) {

 

                break;

                }

                }

 

                i = PositionA + 1;

 

while (i < ExpressionToCalculate.length()) {

                try {

                i++;

                SignB = Integer.parseInt(ExpressionToCalculate.substring(PositionA + 1, i));

                } catch (NumberFormatException exception) {

 

 

                break;

                }

                }

 

if (StringCalculation.equals("*")) {

 

                val = SignA * SignB;

 

                provisional = SignA.toString() + StringCalculation + SignB.toString();

 

                ExpressionToCalculate = ExpressionToCalculate.replace(provisional, val.toString());

                }

 

else if (StringCalculation.equals("/")) {

 

                val = SignA / SignB;

 

                provisional = SignA.toString() + StringCalculation + SignB.toString();

 

                ExpressionToCalculate = ExpressionToCalculate.replace(provisional, val.toString());

 

                }

                }

 

for (  ;  ExpressionToCalculate.contains("+") || ExpressionToCalculate.contains("-")  ;  ) {

 

  postive = ExpressionToCalculate.indexOf("+");

  negative = ExpressionToCalculate.indexOf("-");

 

if (negative == 0) {

 

                break;

 

                }

 

else if (postive == -1) {

 

                StringCalculation = "-";

 

                }

 

else if (negative == -1) {

                StringCalculation = "+";

                }

 

else if (postive < negative) {

 

                StringCalculation = "+";

 

                }

 

else if (negative < postive) {

 

                StringCalculation = "-";

 

                }

 

                PositionA = ExpressionToCalculate.indexOf(StringCalculation);

                SignA = 0;

                SignB = 0;

                int i = PositionA;

 

while (i > 0) {

                try {

                i--;

                SignA = Integer.parseInt(ExpressionToCalculate.substring(i, PositionA));

                } catch (NumberFormatException exception) {

 

 

                break;

 

 

                }

                }

 

                i = PositionA + 1;

 

for ( ; i < ExpressionToCalculate.length() ; ) {

 

                try {

                i++;

                SignB = Integer.parseInt(ExpressionToCalculate.substring(PositionA + 1, i));

 

                } catch (NumberFormatException exception) {

 

                break;

 

                }

                }

 

if (StringCalculation.equals("+")) {

 

                val = SignA + SignB;

 

                provisional = SignA.toString() + StringCalculation + SignB.toString();

 

                ExpressionToCalculate = ExpressionToCalculate.replace(provisional, val.toString());

 

                }

 

else if (StringCalculation.equals("-")) {

 

                val = SignA - SignB;

 

                provisional = SignA.toString() + StringCalculation + SignB.toString();

 

                ExpressionToCalculate = ExpressionToCalculate.replace(provisional, val.toString());

 

                }

 

                }

return  expression + " = " + ExpressionToCalculate +"    " ;

                }

 

 

 

 

}

 

 

 

Input.txt

 

3*5 3-5 5/5*2+8/2+5

 

 📩 Need a similar solution? Email me: adel455@hotmail.com


Nepali Graphics - Learn design, Animation, and Progrmming

Previous Post Next Post