Can someone help me with an 'if' statement?
I got a code that inserts value from jTextField into my database:
Code:
try {
String sql = "INSERT into Sablonas (sablono_pav) values (?)";
pst = conn.prepareStatement(sql);
pst.setString(1, treciaspanelsablonopavTextField.getText());
pst.execute();
treciaspanelinfoLabel.setText("Pranesimas sėkmingai išsaugotas");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
Can someone help me write an IF statement to check if that field is empty?
Something like this
Code:
if ( treciaspanelsablonopavTextField != "") {
try {
String sql = "INSERT into Sablonas (sablono_pav) values (?)";
pst = conn.prepareStatement(sql);
pst.setString(1, treciaspanelsablonopavTextField.getText());
pst.execute();
treciaspanelinfoLabel.setText("Pranesimas sėkmingai išsaugotas");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
} else {
}
Also, if i have multiple fields, can i write something like this?
Code:
if (jTextField1 || jTextfield2 ||jTextField3 != "" {
} else {
}
Re: Can someone help me with an 'if' statement?
String has an isEmpty method.
So just check the String returned by getText().
And no, you have to check each one, so:
Code:
if (jTextField.getText().isEmpty() || jTextField2.getText().isEmpty()) etc etc
Re: Can someone help me with an 'if' statement?
Thank you.
Also maybe you can help me with this - what if i have a field that only numbers can be written in it? is there something similar like isEmpty? like isNumeric or isNotNumeric?
Re: Can someone help me with an 'if' statement?
You can construct a java.utl.Scanner with the text String and query its hasNextXxx() methods. You can also use a JFormattedTextField or write a DocumentFilter for a JTextField's Document to ensure that only digits can be entered (and maybe a single decimal point, if you're dealing with floating point numeric types).
db
Re: Can someone help me with an 'if' statement?
Quote:
Originally Posted by
DarrylBurke
You can construct a java.utl.Scanner with the text String and query its hasNextXxx() methods. You can also use a JFormattedTextField or write a DocumentFilter for a JTextField's Document to ensure that only digits can be entered (and maybe a single decimal point, if you're dealing with floating point numeric types).
db
Thanks, ill look into this