Here, we will discuss the Arithmetic expression having operators like addition and multiplication and you will see how you can take an expression and can calculate the value with the help of the program.

Given an expression having only + and * operators, the task is to evaluate the String expression and find its output. In this, it’s a simple java program to implement in which expression containing only addition(+) and multiplication(*) operators.

**Example 1:**

Input:1+2*3+4*5*6Output:127Explanation:According to BODMAS, multiplication is evaluated first giving an expression as: 1+6+120. Finally the sum is 127.

**Example 2:**

Input:2*3*4Output:24

**Example 3:**

This is how below given java program will take the string as input and split on the basis of operators like addition(+) and multiplication(*).

1+2*3+4*5*6 |1|+|2*3|+|4*5|*6| |1|+|6|+|20|*6 |1|+|6|+|120| =127

**Approach :**

- Split the string with regex matching
**+**sign - Thus, every element in the resultant String array has either a single number or an expression of products.
- Now traverse through the array to find indices having products.
- Split the product expression with regex matching * sign
- Now multiply every number that was split using * sign in Step 4.
- Finally, every index has only numbers that need to be added for the final evaluation of an expression.
- Add integers at all indices in the array received after Step 1.
- The sum gives the final result of the expression string.

Below is the implementation of the above approach:

## Java

`import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `calculator(String str) ` ` ` `{ ` ` ` `// Split expression string using + operator. ` ` ` `// Now every index will have either an integer or an ` ` ` `// expression of only products. ` ` ` `String[] arr = str.split(` `"\\+"` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) { ` ` ` `int` `result = ` `1` `; ` ` ` ` ` `// If index contains * sign, ` ` ` `// split expression using * ` ` ` `if` `(arr[i].contains(` `"*"` `)) { ` ` ` `String[] num = arr[i].split(` `"\\*"` `); ` ` ` ` ` `// Multiply each number separated by * and ` ` ` `// store final product in 'result' variable ` ` ` `for` `(` `int` `j = ` `0` `; j < num.length; j++) { ` ` ` `result *= Integer.parseInt(num[j]); ` ` ` `} ` ` ` ` ` `// Store resultant product value in the ` ` ` `// array index. For ex: index having 2*3 is ` ` ` `// replaced by 6 and index having 4*5*6 is ` ` ` `// replaced by 120. ` ` ` `arr[i] = String.valueOf(result); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Now every array index has a ` ` ` `// single integer as element. ` ` ` `int` `len = arr.length; ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Calculate the sum of all elements ` ` ` `// of array to get final result. ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) { ` ` ` `sum += Integer.parseInt(arr[i]); ` ` ` `} ` ` ` ` ` `// Return the final result ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Expression string is: 1+2*3+4*5*6 ` ` ` `System.out.println(calculator(` `"1+2*3+4*5*6"` `)); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output :**

127

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Evaluate the Value of an Arithmetic Expression in Reverse Polish Notation in Java
- Java Arithmetic Operators with Examples
- Check if a string contains only alphabets in Java using Lambda expression
- Interesting facts about Increment and Decrement operators in Java
- Java Numeric Promotion in Conditional Expression
- Java Lambda Expression with Collections
- How to validate identifier using Regular Expression in Java
- Finding Data Type of User Input using Regular Expression in Java
- Java Program to Find Duplicate Words in a Regular Expression
- Bitwise right shift operators in Java
- Basic Operators in Java
- Java | Operators | Question 1
- Java | Operators | Question 2
- Java | Operators | Question 3
- Java | Operators | Question 4
- Java | Operators | Question 5
- Java | Operators | Question 6
- Java | Operators | Question 7
- Java | Operators | Question 8
- Java | Operators | Question 9

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.