• 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

blank fields

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic