If have a java program with 4 JTextField. In my JButton add, the event code goes like this:

add.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				try {
					
					String add = "insert into person (firstName,middleName,familyName,age) values ('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].getText()+")";
					st.execute(add); //Execute the add sql
					
					Integer.parseInt(inputs[3].getText()); //Convert JTextField Age in to INTEGER
					
					JOptionPane.showMessageDialog(null,"Item Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
					
				}catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null,"Please enter an integer on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
				}catch (Exception ei) {
					JOptionPane.showMessageDialog(null,"Failure to Add Item. Please Enter a number on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);

The code above is 100% working, it detects if the field "Age" is not an integer or if it is empty. Now the problem is I want to detect also if the other fields are empty which means all fields must have an inputs, if one field is empty a JOptionPane message will display "All Fields must be filled up". I've already tried using the if condition like this

add.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				if (inputs[0].getText() == "" || inputs[1].getText() == "" || inputs[2].getText() == "")
					JOptionPane.showMessageDialog(null,"Fill up all the Fields","Error Input", JOptionPane.ERROR_MESSAGE);
					
				else
					
				try {
					
					String add = "insert into person (firstName,middleName,familyName,age) values ('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].getText()+")";
					st.execute(add); //Execute the add sql
					
					Integer.parseInt(inputs[3].getText()); //Convert JTextField Age in to INTEGER
					
					JOptionPane.showMessageDialog(null,"Item Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
					
				}catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null,"Please enter an integer on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
				}catch (Exception ei) {
					JOptionPane.showMessageDialog(null,"Failure to Add Item. Please Enter a number on the Field AGE","Error Input", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);

but it didn't work. Even if the other fields are empty it will still save. I just want to know what is the problem of my if condition.

Recommended Answers

All 3 Replies

In first code th reason it works is due to the fact that you are trying to convert it to an integer and catching the exception. This would happen even if the text field is empty as anything other than a number would throw the exception.

In your if condition in addition for the check that you do for "" also check for null.

if (inputs[0].getText().equals("") || inputs[1].getText().equals("") || inputs[2].getText().equals("") || inputs[0].getText() == null || inputs[1].getText() == null || inputs[2].getText() == null)

As null is not equal to "".

Use equals() method. Do not use == (equality) operator to compare objects of String class.

if (str==null || str.trim().equals(""))
{
 // ...
}

YES!!! It worked, thank you so much, there is so much I need to know about the differences. What I really thought is that "" is equal to null. .now I know. .Thank you, finally solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.