• 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

If-then conversion of textfields for calc

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the problem:
I have to convert information inputted into a textField to a variable format for calculations. I have to have error correction to ensure that information is inputted for each field. Further, I have two buttons and the problem is arrising in the calcButton section. I attempted to do this problem this way:
1st) Convert the three textField objects to String, and a double and double value.
2nd) Ran a test to ensure that the data in the variables contained information, if not give the appropriate error message.
3rd) Did the Calculations.
The problem is that the program attempts to convert empty information to a string or double without testing it first and I get errors.
I'm having a devil of a time nesting this dang thing properly. I know the problem is in the nesting of the if-else if statements and I can't see the code to fix it. Here is the example code, any suggestions?
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ October 01, 2002: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
Note that you can edit your own posts - just click on the icon that looks like a piece of paper.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If the hoursWorked textfield is empty and u try to convert it into a double value then
double hoursWorked = new Double(hoursWorkedField.getText().trim()).doubleValue()
will definitely give u an exception..
Why dont u check whether the text fields r empty first??
If they r empty then give the appropriate error msg and if they r not empty then u can convert them to a double value.
i.e.,
String name = empNameField.getText();
String hrs = hoursWorkedField.getText;
and similarly for the 3rd text field.
if(name.length() == 0 )
{
// err msg;
}
if(hrs.length() == 0)
{
// err msg;
}
else
{
new Double(hoursWorkedField.getText().trim()).doubleValue();
}
and so on..
I really havent ran ur code and seen it so maybe what i have told might not be sufficient enough to debug ur code completely..
Anyway, hope this helps u
 
reply
    Bookmark Topic Watch Topic
  • New Topic