This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Android Studio - Building a Calculator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, guys. How are you?

This is my first post and I'm still working around on the Forum. Sorry if this is the wrong place to post this.

I'm having some issues with an exercise I have to deliver tomorrow - 09/02, where I have to build a calculator. Bellow you will find most of the code that I wrote. Still, I'm having the problems below:

*Can't make it show the operation signs;
*The calculator has to start over after pressing the = but mine gets 0.0 and it adds this to the next number - cannot do that;
- with this error we also have the decimal one: If you use 0.0 and add a number (example: 5), it will be 0.05. If you include a DOT - 0.05.5 - the program crashes.
*I can't make it work as a chain operation, I.e (2+3+5 = 10) in my calculator it goes (2+3+5=8) it forgets the first number;

I spent my whole weekend trying to figure this out. It's probably something simple and if it's possible, an explanation of what I was doing wrong so I can also learn with that.

Thank you very much!

 
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, too much for me in too short a time. And without the entire project, it's hard to debug. It would be easier if you just asked about a snippet. A couple comments though. But beware, i'm just a beginner in java and android, so, maybe we'll learn together.

1) The code seems to care if there is a decimal place. Shouldn't the double handle that on its own?
2) The code goes through a lot of trouble setting up an onClickListerner. Why not just add onClick to the XML for each button? It's be cleaner in that you could get rid of those arrays and the for each loops.
3) Why "(String)((Button) v).getText();" and not "((Button) v).getText().toSring();"

In a pinch, you could "cheat" and have a variable store the current number instead of reading it from the display each time.
 
Ric Cristof
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Tkatch wrote:Sorry, too much for me in too short a time. And without the entire project, it's hard to debug. It would be easier if you just asked about a snippet. A couple comments though. But beware, i'm just a beginner in java and android, so, maybe we'll learn together.

1) The code seems to care if there is a decimal place. Shouldn't the double handle that on its own?
2) The code goes through a lot of trouble setting up an onClickListerner. Why not just add onClick to the XML for each button? It's be cleaner in that you could get rid of those arrays and the for each loops.
3) Why "(String)((Button) v).getText();" and not "((Button) v).getText().toSring();"

In a pinch, you could "cheat" and have a variable store the current number instead of reading it from the display each time.



Hello, Brian.

Thanks for answering. I already deliver the project, but I'm still trying to figure it out the problem.
I'm also a beginner and would be nice to share and learn more.

So, questions:

1 - Decimal place. Double handles that just fine. I have a friend that used Float and got a large number on the answer.
2 - Yeah.... I noticed that after looking for a while on the Internet. It would be waaaay easier to use onClick on the XML. You're right. I just used the hard way - more coding, less design.
3 - You got me on that one. Had to look out. Thanks for asking. ((Button) v).getText().toSring(); it says that cannot resolve the method .toString();. Still, I understand that .getText() gets the information from the button instead of .toString(); where it gets the info from the XML. (Come to think about, I could really used that instead of my method...)

I made a few changes since yesterday and will post later updating the post.
 
Brian Tkatch
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ric Cristof wrote:
1 - Decimal place. Double handles that just fine. I have a friend that used Float and got a large number on the answer.
2 - Yeah.... I noticed that after looking for a while on the Internet. It would be waaaay easier to use onClick on the XML. You're right. I just used the hard way - more coding, less design.
3 - You got me on that one. Had to look out. Thanks for asking. ((Button) v).getText().toSring(); it says that cannot resolve the method .toString();. Still, I understand that .getText() gets the information from the button instead of .toString(); where it gets the info from the XML. (Come to think about, I could really used that instead of my method...)

I made a few changes since yesterday and will post later updating the post.



1. Double has twice the precision of float. While neither is accurate, for your calculator, i doubt it matters. Just round the (longer) numbers. Do a google search for "what every computer scientist should know about floating point"

2. Heh. So many books don't even teach this method, instead showing callbacks and what you did. BTW, there's another way i found, and i'm using in my current project:

- In the class declaration, add: implements View.OnClickListener
- In the class block implement: public void onClick(View view){}
- for the button: button.setOnClickListener(this);

The "this" refers to the class, as you'd expect, making it search for the onClick(). It works well in my scenario because i am creating a bunch of buttons dynamically.

3. Oops! Android Studio didn't allow me to mess with the code because it wasn't part of my project. Based on the (String) i just assumed that it needed to be cast, in which case, there's probably be a toString(). There usually is.

When you post again, please use the code tags. The "Code" button on top of the editor window will make it look nice and pretty, keep formatting, making it much easier to read.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic