Evaluate an array expression with numbers , + and | Java program to find sum of given array of string type in integer form ; Function to find the sum of given array ; if string is empty ; parseInt function to convert string into integer ; parseInt function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code
import java . io . * ; class GFG { public static int calculateSum ( String arr [ ] , int n ) { if ( n == 0 ) return 0 ; String s = arr [ 0 ] ; int value = Integer . parseInt ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = Integer . parseInt ( s ) ; char operation = arr [ i - 1 ] . charAt ( 0 ) ; if ( operation == ' + ' ) sum += value ; else sum -= value ; } return sum ; } public static void main ( String [ ] args ) { String arr [ ] = { "3" , " + " , "4" , " - " , "7" , " + " , "13" } ; int n = arr . length ; System . out . println ( calculateSum ( arr , n ) ) ; } }