Can anyone tell why my code isn't working??(java) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone tell why my code isn't working??(java)

https://code.sololearn.com/cQ53Y6ytcBOc/?ref=app if the input is 30 (10*2) (10*3) (10*4) it is supposed to print out index 1 but my code doesn't operate when the same math operation symbol is repeated. It calcuates (10*2) and then can't find (10*3). It works fine when different symbols are used. I thought my 'for' sentence would find any repeated symbols but it doesn't... Maybe I have a problem with 'if' sentence?? I'm working on this code coach question for three days and still can't find the solution.... I'll appreciate any comments!!!

18th Nov 2022, 11:06 AM
이참치
이참치 - avatar
7 Answers
+ 3
Try print the values mp1 and mp2 you will see these variables hold the previous values and then you append the new. For example input: 30 (10*2) (10*3) the 1st time mp1=10, mp2=2 but at the 2nd mp1=1010, mp2=23 same for d1,d2 for division etc...
18th Nov 2022, 12:36 PM
Giannis
Giannis - avatar
+ 2
Wow it is working!!!!!! I initialized StringBuilder and it was fixed!!! Thank you!!! your comment helped me so much :D
18th Nov 2022, 12:47 PM
이참치
이참치 - avatar
+ 1
The d1 and d2 hold the 1st and 2nd number of expression?
18th Nov 2022, 12:28 PM
Giannis
Giannis - avatar
+ 1
I used different StringBuilder in different math operation symbols. so num1 num2(+) m1 m2(-) mp1mp2(*) d1d2(/) I designed my code to find divide(/) and then StringBuilder d1 holds numbers front of '/' and StringBuilder d2 holds numbers back of '/' and then used parseInt to calculate them!
18th Nov 2022, 12:36 PM
이참치
이참치 - avatar
+ 1
oh thank you so much!!! I'll check it right away
18th Nov 2022, 12:38 PM
이참치
이참치 - avatar
0
Im so sorry I can't understand you comment... you mean my program only takes (10*2) and can't take (10*3) problem has to do with sc.nextLine()?? but when I order to print int num(first sc.nextLine) and input[1](second sc.nextLine and splitted into an array) it prints them out properly.... so i can't find out what I should fix. but I appreciate your comment!
18th Nov 2022, 12:05 PM
이참치
이참치 - avatar
0
you need to clear them before re-using them
18th Nov 2022, 12:46 PM
Giannis
Giannis - avatar