Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Textfield Problem

 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2147483647 is Integer.MAX_VALUE; perhaps you should think about using long or even BigInteger for your results.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • 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
  • 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.. )
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic