QUESTION

Text
Image

Need Java Solution ASAP, attached JavaScript solution as a reference


String Challenge Have the function StringChallenge (str) read the str parameter being passed which will contain the written out version of the numbers 0-9 and the words "minus" or "plus" and convert the expression into an actual final number written out as well. For example: if str is "foursixminustwotwoplusonezero" then this converts to "46 $22+10$ " which evaluates to 34 and your program should return the final string threefour. If your final answer is negative it should include the word "negative." Examples Input: "onezeropluseight" Output: oneeight Input: "oneminusoneone" Output: negativeonezero import java.util.*; import java.io.*; class Main \{ public static String StringChallenge(String str) \{ // code goes here return str; \} public static void main (String[] args) \{ // keep this function call here Scanner $\mathbf{s}$ = new Scanner(System.in); System. out . print (StringChallenge (s . nextLine())); \} 子
function StringExpression(str) \{ let newstring $=$ str.slice $(\theta)$; let dictionary $=[$ ['zero', '0'], ['one', '1'], ['two', '2'], ['three', '3'], ['four', '4'], ['five', '5'], ['six', '6'], ['seven', '7'], ['eight', '8'], ['nine', '9'], ['minus', '-'], ['plus', '+'] ]; dictionary.forEach (val $\Rightarrow\{$ let regEx = new RegExp (val[0], 'g'); newString = newString.replace(regEx, val[1]); \}); let resstring $=$ eval (newstring), toString(); dictionary.slice $(0,10)$. forEach (val $\Rightarrow\{$ let regEx = new RegExp (val[1], ' $g ')$; resString $=$ resString. $\operatorname{replace}(\operatorname{regEx}, \operatorname{val}[0])$; \}); return resString. replace('-', 'negative'); \} // keep this function call here StringExpression(readline());

Public Answer

PRBGRW The First Answerer