• 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

Textfield Problem

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I am trying to make a simple calculator.. everything works but when i tried to input more than 11 numbers in my textfield then press the operators the numbers in the ext field CHAnGES!! I have no idea why!

Can please anybody healp me? Thanks

Sample input: 123456789111 Then if I press one of the operators it will change to 2147483647..



[ September 19, 2008: Message edited by: Rob Prime ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2147483647 is Integer.MAX_VALUE; perhaps you should think about using long or even BigInteger for your results.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Beth Laguardia:
Hi everyone, I am trying to make a simple calculator.. everything works but when i tried to input more than 11 numbers in my textfield then press the operators the numbers in the ext field CHAnGES!! I have no idea why!



I haven't actually run your code, but I would guess that it's the
normal limitations of the double type that java (and many
other languages) uses. There are some values that it just can't
represent, and 123456789111 is one of them. If you don't like
this, you could try using BigDecimal instead.

While I'm here, allow me ask a couple things about your code:

1) Why bother to use String variables b0, b1, etc? (I would just use
the String literals "0", "1", etc. but if you really want to use those
variables, perhaps you should declare them to be static final.)

2) Is there a reason you're doing setLayout(null) and manually
calling setBounds() on everything?
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:


I haven't actually run your code, but I would guess that it's the
normal limitations of the double type that java (and many
other languages) uses. There are some values that it just can't
represent, and 123456789111 is one of them. If you don't like
this, you could try using BigDecimal instead.

While I'm here, allow me ask a couple things about your code:

1) Why bother to use String variables b0, b1, etc? (I would just use
the String literals "0", "1", etc. but if you really want to use those
variables, perhaps you should declare them to be static final.)

2) Is there a reason you're doing setLayout(null) and manually
calling setBounds() on everything?




Thank you for explanation. I do not know yet how use BigDecimal but will check on it.

With using the string literals, I will denitely change it string literals. Thanks for that input.

I have no idea yet what is static final, but i have check on it also.

I have set the frame to null because if i remove if all my buttons will be gone. Is there a better way to do this?

Thank you all or the response.. )
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Beth Laguardia:
I have set the frame to null because if i remove if all my buttons will be gone. Is there a better way to do this?



Most definitely. Please have a look at the Sun tutorial on the AWT layout managers. If you let the layout managers do the hard work for you, laying your components, your program will be much more flexible and maintainable.
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic