• Post Reply Bookmark Topic Watch Topic
  • New Topic

blank fields  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had no trouble validating/exception handling textfields where doubles are put in (i.e if there is a number there, my program will be able to tell if it is too high or low).

My problem is that if no value is put in the textfield, my program crashes. How would I handle this exception?

double height = Float.parseFloat(jTextField6.getText());

How can i make sure that if the user leaves this field blank, the field will automatically be re-set to 0 and an error message will display?

 
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

In my opinion, you're trying to kill a ant with a atomic bomb. I mean... If you're going to use exceptions for every code that a simple if resolves the problem, the system'll be complex.

I guess that, if I had the same question, I would treat the empty field with a simple if. If it is blank, show the alert message (or any other kind of message), saying that the default value is zero. Then, if it is blank, force that textfield to receive a zero.

Sorry my english.
Hug.
 
Shrey Puranik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andre Brito wrote:Hi.

In my opinion, you're trying to kill a ant with a atomic bomb. I mean... If you're going to use exceptions for every code that a simple if resolves the problem, the system'll be complex.

I guess that, if I had the same question, I would treat the empty field with a simple if. If it is blank, show the alert message (or any other kind of message), saying that the default value is zero. Then, if it is blank, force that textfield to receive a zero.

Sorry my english.
Hug.


I agree-I just dont know how to code it:

My presumed pseudo code would be:

if (field == "NULL"){
field.setText(String.valueOf("0"));
errorBox.setText"String.valueOf("Please enter a valid value: ));

its the bit where the field == NULL-how is this written in java?
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Almost that.
Suppose that you have a JTextField name textNumber and a Double called number. So, you'll have to verify that. The easiest way to do it is that (I believe so, but not the more appropriate*):


*Not more appropriate 'cause the system you presente some errors if the user type some spaces. You can try to verify it using some regular expressions or even a method already implemented from the API (Swing is not my forte).

Actually, I believe that there's a NullPointerException ready for do that (take a look at the API).

Hug.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, Java is pass by reference, not by value. By saying "field == null" that would be checking if the field is a null reference. What I would do is something like this:



Would something like that suffice?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jacob Steingart; in fact you could simply set the text to "0.0" when you set up the field.

By the way: Please use the code button rather than writing "[code]" by hand.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!