Reputation: 21
I am a java beginner, i wrote this application using netbeans. Its purpose is to get the entered value from the textbox and display it in a message box when i press the button. It works fine when i give the value in the textbox. When i press the button with out giving a value it is suppose to display nothing, but it displays an empty message box. So please help me to fix the problem.
private void ChecktheloopActionPerformed(java.awt.event.ActionEvent evt) {
String recno= numsearch1.getText();
String srname=searchname.getText();
if(recno!=null){
JOptionPane.showMessageDialog(null,recno);
}
}
Upvotes: 1
Views: 149
Reputation: 4511
You have to check if text is an empty string, using isEmpty()
method of String class:
private void ChecktheloopActionPerformed(java.awt.event.ActionEvent evt) {
String recno= numsearch1.getText();
String srname=searchname.getText();
if(!recno.isEmpty()){
JOptionPane.showMessageDialog(null,recno);
}
}
Upvotes: 0
Reputation: 3840
You also have to check if the string is equals to ""
before showing the dialog box.
private void ChecktheloopActionPerformed(java.awt.event.ActionEvent evt) {
String recno= numsearch1.getText();
String srname=searchname.getText();
if(!recno.equals("")){
JOptionPane.showMessageDialog(null,recno);
}
}
numsearch1.getText();
is probably returning an empty string instead of the null
you were expecting.
Upvotes: 0
Reputation: 1108
try it
private void ChecktheloopActionPerformed(java.awt.event.ActionEvent evt) {
String recno= numsearch1.getText();
String srname=searchname.getText();
if(recno!=null && recno.trim().length()>0){
JOptionPane.showMessageDialog(null,recno);
}
}
Upvotes: 1