Arka3L
asked on
Check TextField for null is throwing error
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.NumberFormatExce ption: empty String
at sun.misc.FloatingDecimal.r eadJavaFor matString( FloatingDe cimal.java :994)
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.NumberFormatExce
at sun.misc.FloatingDecimal.r
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));
}
}
You should first validate the fields before convert it to float
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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!
Thanks for the help Ajay!
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(jTextFiel d2.getText ());
} catch(ParseFailedException e) {
// Incorrect input
valid = false;
JOptionPane.showConfirmDia log((Compo nent)
null, "Please enter a value for pounds", "alert", JOptionPane.OK_CANCEL_OPTI ON);
}
if(valid) {
// parse rate
try {
rate = Float.parseFloat(jTextFiel d1.getText ());
} catch(ParseFailedException e) {
valid = false;
// Incorrect input
JOptionPane.showConfirmDia log((Compo nent)
null, "Please enter a value for rate", "alert", JOptionPane.OK_CANCEL_OPTI ON);
}
}
if(valid) {
result = pound*rate;
jTextField3.setText(String .valueOf(r esult));
}
float pound, rate, result;
boolean valid = true;
try {
pound = Float.parseFloat(jTextFiel
} catch(ParseFailedException
// Incorrect input
valid = false;
JOptionPane.showConfirmDia
null, "Please enter a value for pounds", "alert", JOptionPane.OK_CANCEL_OPTI
}
if(valid) {
// parse rate
try {
rate = Float.parseFloat(jTextFiel
} catch(ParseFailedException
valid = false;
// Incorrect input
JOptionPane.showConfirmDia
null, "Please enter a value for rate", "alert", JOptionPane.OK_CANCEL_OPTI
}
}
if(valid) {
result = pound*rate;
jTextField3.setText(String
}