Reputation: 17
I have 2 methods to get protections from jTextField18.gettext
a string
First method:
public boolean isEditValid(String number) {
for (int i = 0; i <= number.length(); i++)
if (number.indexOf(i) <= 'a' && number.indexOf(i) >= 'z')
return true;
return false;
}
Second method:
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField18.getText().isEmpty())
JOptionPane.showMessageDialog(this, "The text field is emty");
else if (isEditValid(jTextField18.getText()) == true)
JOptionPane.showMessageDialog(this, "Edit isnt valid ONLY NUMBERS!");
}
When i write on text field a number and a string its says me Edit isnt valid ONLY NUMBERS
why?
Upvotes: 0
Views: 50
Reputation: 8395
as currently written, isEditValid()
returns false
if all characters are lower case, and true otherwise.
so i suspect it is not doing what you expect.
i recommend you build a mcve and test different parameters for isEditValid()
also, should it be
for (int i = 0; i < number.length(); i++)
instead ? (i have not done any java for a while...)
Upvotes: 0
Reputation: 1281
If you want to validate a number, you can use betters ways to do that, for example (using regular expression):
public static boolean isEditValid(String number) {
return number.matches("^[0-9]+$");
}
Upvotes: 1