Reputation: 21
I need to perform arithmetic operations on numbers in a string. I understand that I'll need to parse those numbers out, convert to int, do the math, and then convert back to String.
Example:
n = 2.
Input data: "The building is 350 feet tall with a perimeter of 400 feet".
Output data: The building is 352 feet tall with a perimeter of 402 feet".
I got my for loop iterating through the string and I can affect the value of the number, but I'm having difficulty putting it back into a new, modified string.
Here's where I'm at right now but it's not working
String output = new String(); String tempString = new String(); int newNumb = 0; int n = 2; for (int i = 0; i < output.length(); i++){ Pattern integerPattern = Pattern.compile('-?\\d+'); Matcher matcher = integerPattern.matcher(output); newNumb = (Integer.parse(output) + n); tempString = output.replace(output, String.valueOf(newNumb));
Upvotes: 0
Views: 53