Hi
I am trying below code in textbox input values like below
8-B
12-A
[20-C
4-B]5
-------
Sum = 140
----------
if i want to change to 15 or 25 or whatever like between 11-99 in place of 5 then error will come like below image.
Note: if i changeTimng between 1 - 9 then is working perfectly.the problem is after between change 10-99 then error is comming.

below is my code.
C#
private void button1_Click(object sender, EventArgs e)
{
int combinedSum = 0;
for (char letter = 'A'; letter ();
int stackSum = 0;
foreach (string line in textBox1.Lines)
{
string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces
if (temp.Count() == 0) continue; // ignore blank line
bool containsLetter = (line.IndexOf(letter) > -1);
char first = temp.First();//temp[0];
char last = temp.Last();//temp[temp.Length - 1];
if (Char.IsDigit(first))
{
string numStr = temp.Split('-')[0];
if (!containsLetter) numStr = "0";
if (stack.Count == 0)
{
sum += int.Parse(numStr);
}
else
{
stack.Push(numStr);
}
if (Char.IsDigit(last))
{
int multiplier = last - 48;
char bracket = temp[temp.Length - 2];
int total = 0;
char openChar = '\0';
string tempStr;
if (bracket == '}')
openChar = '{';
else if (bracket == ']')
openChar = '[';
else if (bracket == ')')
openChar = '(';
else if (bracket == '>')
openChar = '')
openChar = '<';
do
{
tempStr = stack.Pop();
int num;
if (int.TryParse(tempStr, out num)) total += num;
}
while (tempStr[0] != openChar);
stackSum += (stackSum + total) * multiplier;
if (stack.Count == 0)
{
sum += stackSum;
stackSum = 0;
}
}
}
}
combinedSum += sum;
}
textBox6.Text = String.Format("Sum = {0}", combinedSum);
}Please correct above code and resolve this problem.
Thanks in Advance

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 2, 2024, 3:26 PM EDT
Hi Luv,
Here is the updated version of code which handled all your scenarios which shared in last comments and in the attached input file. Now, calculation is working fine with single and double digit.
private void button1_Click(object sender, EventArgs e) { int combinedSum = 0; for (char letter = 'A'; letter (); int stackSum = 0; foreach (string line in textBox1.Lines) { string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces if (temp.Count() == 0) continue; // ignore blank line bool containsLetter = (line.IndexOf(letter) > -1); char first = temp.First();//temp[0]; char last = temp.Last();//temp[temp.Length - 1]; if (Char.IsDigit(first)) { string numStr = temp.Split('-')[0]; if (!containsLetter) numStr = "0"; if (stack.Count == 0) { sum += int.Parse(numStr); } else { stack.Push(numStr); } if (Char.IsDigit(last)) { string last2 = ""; if (temp.IndexOf("]") > 0 || temp.IndexOf("}") > 0) { if(temp.IndexOf("]") > 0) last2 = temp.Substring(temp.IndexOf("]") + 1, temp.Length - (temp.IndexOf("]") + 1)); else if (temp.IndexOf("}") > 0) last2 = temp.Substring(temp.IndexOf("}") + 1, temp.Length - (temp.IndexOf("}") + 1)); } int multiplier = 0; if (!String.IsNullOrEmpty(last2) && Convert.ToInt32(last2) > 9) multiplier = Convert.ToInt32(last2); else multiplier = last - 48; int total = 0; char openChar = '\0'; string tempStr; if (temp.IndexOf(">") > 0) openChar = ' 0) openChar = '('; else if (temp.IndexOf("}") > 0) openChar = '{'; else if (temp.IndexOf("]") > 0) openChar = '['; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum = (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } else { stack.Push(first.ToString()); int index = 1; while (true) { if (Char.IsDigit(temp[index])) break; stack.Push(temp[index].ToString()); index++; } string numStr2 = temp.Substring(index).Split('-')[0]; if (!containsLetter) numStr2 = "0"; stack.Push(numStr2); if (Char.IsDigit(last)) { string last2 = ""; if (temp.IndexOf("]") > 0 || temp.IndexOf("}") > 0) { if (temp.IndexOf("]") > 0) last2 = temp.Substring(temp.IndexOf("]") + 1, temp.Length - (temp.IndexOf("]") + 1)); else if (temp.IndexOf("}") > 0) last2 = temp.Substring(temp.IndexOf("}") + 1, temp.Length - (temp.IndexOf("}") + 1)); } int multiplier = 0; if (!String.IsNullOrEmpty(last2) && Convert.ToInt32(last2) > 9) multiplier = Convert.ToInt32(last2); else multiplier = last - 48; int total = 0; char openChar = '\0'; string tempStr; if (temp.IndexOf(">") > 0) openChar = ' 0) openChar = '('; else if (temp.IndexOf("}") > 0) openChar = '{'; else if (temp.IndexOf("]") > 0) openChar = '['; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum += (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } } combinedSum += sum; } textBox6.Text = String.Format("Sum = {0}", combinedSum); }Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Sep 29, 2024, 1:27 PM EDT
Hi Luv,
The issue in your code occurs when you retrieve the bracket in the following line (in both If and Else clause):
char bracket = temp[temp.Length - 2];This works fine for single-digit numbers, but when you enter a two-digit number, it doesn't return the bracket because temp.Length is 6, and subtracting 2 gives the value at index 4, which is '1'. As a result, your do-while loop runs indefinitely since no bracket is found. To resolve this, you need to add a condition to check the length and ensure you're retrieving the correct bracket.
char bracket; if (temp.Length > 5) bracket = temp[temp.Length - 3]; else bracket = temp[temp.Length - 2];Thank you.
Luv LRank #8,316 · 224 reputationPosted Sep 29, 2024, 11:55 PM EDT
Thankyou @Muhammad Imran Ansari
if possible make the correction and send the full code for better idea.
Thankyou.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Sep 30, 2024, 6:35 AM EDT
Sure, here is final code after changes.
private void button1_Click(object sender, EventArgs e) { int combinedSum = 0; for (char letter = 'A'; letter (); int stackSum = 0; foreach (string line in textBox1.Lines) { string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces if (temp.Count() == 0) continue; // ignore blank line bool containsLetter = (line.IndexOf(letter) > -1); char first = temp.First();//temp[0]; char last = temp.Last();//temp[temp.Length - 1]; if (Char.IsDigit(first)) { string numStr = temp.Split('-')[0]; if (!containsLetter) numStr = "0"; if (stack.Count == 0) { sum += int.Parse(numStr); } else { stack.Push(numStr); } if (Char.IsDigit(last)) { int multiplier = last - 48; //char bracket = temp[temp.Length - 2]; char bracket; if (temp.Length > 5) bracket = temp[temp.Length - 3]; else bracket = temp[temp.Length - 2]; int total = 0; char openChar = '\0'; string tempStr; if (bracket == '}') openChar = '{'; else if (bracket == ']') openChar = '['; else if (bracket == ')') openChar = '('; else if (bracket == '>') openChar = ' 5) bracket = temp[temp.Length - 3]; else bracket = temp[temp.Length - 2]; int total = 0; char openChar = '\0'; string tempStr; if (bracket == '}') openChar = '{'; else if (bracket == ']') openChar = '['; else if (bracket == ')') openChar = '('; else if (bracket == '>') openChar = '<'; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum += (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } } combinedSum += sum; } textBox6.Text = String.Format("Sum = {0}", combinedSum); }Luv LRank #8,316 · 224 reputationPosted Sep 30, 2024, 9:14 AM EDT
Thankyou @Muhammad Imran Ansari
Iam tried revised changes code but same above System.InvalidOperationException: 'Stack empty.' problem is comming while iam using single digit timing and double digit timing.without backet and without timing is working perfectly.
so please check and send the revised code for moving my application.
Thankyou
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 1, 2024, 11:43 AM EDT
Hi Luv,
As per my last comments "Can you share the sample input where it's working and where not? so that I can suggest a solution."
Share the sample Inputs
Thank You!
Luv LRank #8,316 · 224 reputationPosted Oct 1, 2024, 1:27 PM EDT
Dear @Muhammad Imran Ansari
Please find theupload sample file
Thanks
Luv LRank #8,316 · 224 reputationPosted Oct 2, 2024, 2:33 AM EDT
Dear @Muhammad Imran Ansari
Thanks for kind help.but single digit is work well.if i change to double digit like 12 then it will caluculated with second digit 2 only not 12.this is the main issue with our code.
i would like to request you to i change my logic to like below.pls help with code.
if i entered input then it will copy to another textbox like below given.
For example-1
TEXTBOX-1
[8-A
8-B]2 =====> (TIMING RANGE BETWEEN 1-999) OR WITHOUT BRACKET
4-C
-----
36
------
ABOVE TEXTBOX INPUT CHANGE TO TEXTBOX-2 LIKE BELOW
TEXTBOX-2
8-A
8-B
8-A
8-B
4-C
-----
36
----
then our problem is solved.
hope you understand my logic.
Thankyou.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Sep 30, 2024, 1:36 PM EDT
Hi Luv,
Can you share the sample input where its working and where not? so that I can suggest a solution.
Luv LRank #8,316 · 224 reputationPosted Oct 1, 2024, 11:15 AM EDT
Thankyou @Muhammad Imran Ansari
isame proble sir its not working.only without bracket is get correct result after said changes.but
if i change to single digit error will come System.InvalidOperationException: 'Stack empty.'
and if i change to double digit result will be 0.
Help with correct code and share full revised code.
Thanks for your kind support
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 1, 2024, 4:58 PM EDT
Hi Luv,
You bracket/exception issue is fixed and here is the complete code. however, there is some calculation issue with 2 digits, that part check because you know the actual logic better than me. :)
private void button1_Click(object sender, EventArgs e) { int combinedSum = 0; for (char letter = 'A'; letter (); int stackSum = 0; foreach (string line in textBox1.Lines) { string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces if (temp.Count() == 0) continue; // ignore blank line bool containsLetter = (line.IndexOf(letter) > -1); char first = temp.First();//temp[0]; char last = temp.Last();//temp[temp.Length - 1]; if (Char.IsDigit(first)) { string numStr = temp.Split('-')[0]; if (!containsLetter) numStr = "0"; if (stack.Count == 0) { sum += int.Parse(numStr); } else { stack.Push(numStr); } if (Char.IsDigit(last)) { int multiplier = last - 48; int total = 0; char openChar = '\0'; string tempStr; if (temp.IndexOf(">") > 0) openChar = ' 0) openChar = '('; else if (temp.IndexOf("}") > 0) openChar = '{'; else if (temp.IndexOf("]") > 0) openChar = '['; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum = (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } else { stack.Push(first.ToString()); int index = 1; while (true) { if (Char.IsDigit(temp[index])) break; stack.Push(temp[index].ToString()); index++; } string numStr2 = temp.Substring(index).Split('-')[0]; if (!containsLetter) numStr2 = "0"; stack.Push(numStr2); if (Char.IsDigit(last)) { int multiplier = last - 48; int total = 0; char openChar = '\0'; string tempStr; if (temp.IndexOf(">") > 0) openChar = ' 0) openChar = '('; else if (temp.IndexOf("}") > 0) openChar = '{'; else if (temp.IndexOf("]") > 0) openChar = '['; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum += (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } } combinedSum += sum; } textBox6.Text = String.Format("Sum = {0}", combinedSum); }Thank you.