JavaRanch » Java Forums »
Java »
Swing / AWT / SWT
| Author |
Textfield Problem
|
Beth Laguardia
Ranch Hand
Joined: Jun 01, 2008
Posts: 34
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
|
2147483647 is Integer.MAX_VALUE; perhaps you should think about using long or even BigInteger for your results.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
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?
|
bitguru blog
|
 |
Beth Laguardia
Ranch Hand
Joined: Jun 01, 2008
Posts: 34
|
|
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
Joined: Feb 23, 2007
Posts: 1561
|
|
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.
|
 |
 |
|
|
subject: Textfield Problem
|
|
|
|