• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write decimal point in calculator?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is  my  calculator. How  to write  here  decimal  point  correctly.
***if(e.getSource()==decPoint ){
    tf.setText(tf.getText()+decPoint );
           }**** I  would  be  grateful fo  your  help.

Here is  my  code:
 
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vital Jays wrote:Here is  my  calculator. How  to write  here  decimal  point  correctly.
.


Close. "decPoint" is a Button but not what you want to add to your text field.
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank  you your  advice, but  I  think  here in the code  should  be  if ... else block,  that  shows  that we  put point to a  number in text field. How write that block here correctly?
 
Carey Brown
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vital Jays wrote:Thank  you your  advice, but  I  think  here in the code  should  be  if ... else block,  that  shows  that we  put point to a  number in text field. How write that block here correctly?

What would your IF be testing for? Why?
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, actually,  i am  not  sure whether if... else is  needed  here, but  I  just  think  that  there  should  be  some  code that  puts point  in  the  text field and make calculations  with "double"  type
 
Carey Brown
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vital Jays wrote:Well, actually,  i am  not  sure whether if... else is  needed  here, but  I  just  think  that  there  should  be  some  code that  puts point  in  the  text field and make calculations  with "double"  type


1) adding a decimal point should not trigger a calculation. What if additional digits will follow the decimal point?
2) all calculations should be done with doubles. What would you expect the answer to "1/3" be? 0 or 0.3333333333?
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I  see that  my  calculator shows 0.0 when we  press 1/3, but  actually it is  the  simplest calculator, and  of  course  it  is  not  perfect.  After your  words  i  understand  that  the  function  of  division works  not  properly
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vital Jays wrote:I  see that  my  calculator shows 0.0 when we  press 1/3, but  actually it is  the  simplest calculator, and  of  course  it  is  not  perfect.  After your  words  i  understand  that  the  function  of  division works  not  properly



To  my  mind there  should  be   Double.parseDouble everywhere in  order to  calculate  it  properly
 
Marshal
Posts: 79232
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vital Jays wrote:. . .
if(e.getSource()==decPoint ){
. . .

I always start to worry when I see that sort of code. It suggests to me that you are not using the Objects which the different buttons represent. You shou‍ld have a value linked to each of the number buttons, pushing a number button adds a number to a value somewhere. Probably a String or similar. The decimal point button operates similarly to a number button, but adding a decimal point converts your number from an integer to a decimal fraction.
There are all sorts of ways to check the type of a number, but I like the following:-That code has, of course, no business being in a GUI class. It shou‍ld be in a Calculation class or similar. You shou‍ld have the calculator working as a command‑line application before you write the GUI.

Why are you using system.exit? That is a dangerous thing to use in GUIs. Why are you using AWT display classes? They were superseded about eighteen years ago.
 
Vital Jays
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank  you  very  much  for your  help and your  variant.

I ve changed Integer to Double everywhere, put  the  code  outside the if(e.getSource()==equal) block? and  used the  following code:
if(e.getSource()==decPoint && !tf.getText().contains("."))
            {
            tf.setText(tf.getText()+"." );
            }

and  the  button  started  to  work
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic