From d35ce7b3901c009556b5729127e6c2595fc085e7 Mon Sep 17 00:00:00 2001 From: Bryan Garland Date: Thu, 16 Jan 2025 14:38:18 -0500 Subject: [PATCH] fix #510 - Trigger syntax error if parenthesis do not match to inform user entry is invalid --- .../darkempire78/opencalculator/activities/MainActivity.kt | 2 ++ .../opencalculator/calculator/parser/Expression.kt | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt b/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt index 85d66391..44c65abb 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt @@ -957,6 +957,8 @@ class MainActivity : AppCompatActivity() { val calculation = binding.input.text.toString() + Expression().addParenthesis(calculation) + if (calculation != "") { val resultString = calculationResult.toString() diff --git a/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt b/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt index 9694d236..04a41c57 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt @@ -102,7 +102,7 @@ class Expression { } - private fun addParenthesis(calculation: String): String { + fun addParenthesis(calculation: String): String { // Add ")" which lack var cleanCalculation = calculation var openParentheses = 0 @@ -121,6 +121,10 @@ class Expression { cleanCalculation += ')' } } + // Trigger syntax error if missing '(' + if (closeParentheses > openParentheses) { + syntax_error = true + } return cleanCalculation }