Solved

Check TextField for null is throwing error

Posted on 2008-06-14
4
3,385 Views
Last Modified: 2012-05-05
Hey guys,

I was literally inches away from getting this working and I dont know where I've gone wrong...

I have made a simple java panel that is used for pounds and dollar conversion depending on rate. So I have a rate field, pound and dollar fields.
I want java to check for a field being empty, if so throw a JOptionPane alert. I got it to work at one point when the fields were default at 0, it was much easier to check a field for the presence of an integer at the time. What am I doing wrong?


Error: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)

float pound, rate, result;
 
pound = Float.parseFloat(jTextField2.getText());
rate = Float.parseFloat(jTextField1.getText());
 
 
if (jTextField1.getText().equals("")){  // If the field is null, request the user to enter a number
 
    JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for rate", "alert", JOptionPane.OK_CANCEL_OPTION);
}
else if (jTextField2.getText().equals("")){ // If the field is null, request the user to enter a number
       JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for pounds", "alert", JOptionPane.OK_CANCEL_OPTION);
}
else
{
result = pound*rate;
jTextField3.setText(String.valueOf(result));
}
}

Open in new window

0
Comment
Question by:Arka3L
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21784905
You should first validate the fields before convert it to float
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 21784908
This should work:
float pound, rate, result;
 
 
if (jTextField1.getText().equals("")){  // If the field is null, request the user to enter a number
 
    JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for rate", "alert", JOptionPane.OK_CANCEL_OPTION);
}
else if (jTextField2.getText().equals("")){ // If the field is null, request the user to enter a number
       JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for pounds", "alert", JOptionPane.OK_CANCEL_OPTION);
}
else
{
pound = Float.parseFloat(jTextField2.getText());
rate = Float.parseFloat(jTextField1.getText());
 
result = pound*rate;
jTextField3.setText(String.valueOf(result));
}
}
 
 
Open in New Window 

Open in new window

0
 
LVL 1

Author Comment

by:Arka3L
ID: 21784943
Wow, I can't believe it was that simple -.-, I will award you the points, but can you explain to me the importance of doing this method? Before I did the check for the field being blank, my floats were above the first if statement and it worked fine. Now that I'm checking the field for being empty I have to add them at the end. I just need to learn a little more before I give the points away otherwise I wont learn :)

Thanks for the help Ajay!
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21784971
In the code you have, you parse the user input (by calling Float.parseFloat) first.  In the case of bad input its going to throw an exception. The validation can be done alongwith you are parsing the input:
 
float pound, rate, result;
boolean valid = true;
 
try {
   pound = Float.parseFloat(jTextField2.getText());
} catch(ParseFailedException e) {
  // Incorrect input
   valid = false;
   JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for pounds", "alert", JOptionPane.OK_CANCEL_OPTION);
}
 
if(valid) {
// parse rate
try {
   rate = Float.parseFloat(jTextField1.getText());
} catch(ParseFailedException e) {
  valid = false;
  // Incorrect input
      JOptionPane.showConfirmDialog((Component)
                null,  "Please enter a value for rate", "alert", JOptionPane.OK_CANCEL_OPTION);
}
}
 
if(valid) {
result = pound*rate;
jTextField3.setText(String.valueOf(result));
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question