Win a copy of Create an Uber Clone in 7 Days: Build a real world full stack mobile app in Java this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculator: can't have two operators in a row  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a calculator but the problem is that I can't make it so that there can't be two operators in a row, for example (4*2++).
The user shouldn't be able to press two operators in a row. My fix was that if the user puts in a "+", i would look for the last character in my >calculation> (holds all the input numbers and operators) string and change that operator according to
which operator the user presses next. Example, if the user puts in, 4*2+, then presses "-", the <calculation> is changed to, 4*2- . This somehow doesn't work. Is there anything I'm missing?

 
Sheriff
Posts: 23608
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, first of all you shouldn't use the == operator to compare whether the contents of two String objects are equal. The == operator compares whether the references point to the same String object, whereas it's easy for two different String objects to contain the same value.

But there's no need to use String objects at all there. I would suggest using char variables instead; a char is a primitive value so the == operator works in the obvious way. Just remember that a character literal is surrounded by single quotes.



I've commented out the third line of code because it has so many problems. It looks like you wanted to replace the + sign by another + sign, which is already a problem, so getting into the other problems with the way you tried to do that would be a waste of time. What did you actually intend to do there?

(The whole code block might be off-course anyway -- you claim to be looking for two operators in a row, but your code is just looking for a + sign at the end. How does that help?)
 
Paul Clapham
Sheriff
Posts: 23608
48
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you used a method called setOnClickListener which isn't part of the standard Java API. It's part of the Android API, though. Are you writing an Android app? If so then we should move this thread to the Android forum. Let us know.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The third line is looking for the last character and if it's a "+" replace it with a "+" so it doesn't add the plus signs.
 
Sheriff
Posts: 12107
198
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ian bilgaen wrote:The third line is looking for the last character and if it's a "+" replace it with a "+" so it doesn't add the plus signs.



That makes as much sense as walking into a room full of people and saying "Ok, I need everyone to listen up! If you're already sitting down, then sit down."
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Junilu Lacar
Sheriff
Posts: 12107
198
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those times when separation of concerns really helps. Parsing user input should be a separate concern from obtaining user input. Mixing those two concerns in one method really makes your design more brittle, more difficult to test, and less cohesive.
 
Junilu Lacar
Sheriff
Posts: 12107
198
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The user can only use an operator once, followed by numbers. How do I do this?
 
Paul Clapham
Sheriff
Posts: 23608
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ian bilgaen wrote:

The user can only use an operator once, followed by numbers. How do I do this?



Well, earlier I asked whether you were writing a Java app or an Android app. You haven't answered yet. If it's Java then you're going to have to explain where the setOnClickListener method and the View class come from, as they aren't part of the standard Java API.
 
Junilu Lacar
Sheriff
Posts: 12107
198
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I should have mentioned that this last post from OP was merged in from a topic that said it's an Android Studio project.
 
Paul Clapham
Sheriff
Posts: 23608
48
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I've moved the thread to the Android forum where people who don't know what a ClickListener does won't be tempted to provide misleading advice.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Your buns are mine! But you can have this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!