just wondering if anyone could explain this...

if(loginF.equals("root") && passwordF.equals("root"));  ---> if this semicolon is here, my program works.. but i can't have an else statement
if i take out this semicolon, the code within the if statement don't execute.. but i can then have an else statement ... no exceptions though

this is the entire method

public void actionPerformed(ActionEvent e)
	{
	
		if(e.getSource() == submitB)
		{
			try
			{
				//if(loginF.equals("root") && passwordF.equals("root"));
				if(loginF.getText()=="root" && passwordF.getText() == "root");
				{
					mb = new MenuBar(this);
					mb.setBackground(java.awt.Color.white);
					this.setJMenuBar(mb); 
					this.validate();
					this.repaint();
					setBounds(400, 400, 600, 500);
					setVisible(true);
				}
			
				/*
				else //not allowed because of the ; above
				{
					JOptionPane.showMessageDialog(null, "wrong password!");
				}
				*/
			}// end try
			
			catch(Exception x)
			{
				System.out.println(x.toString());
			}
			/*
			else
			{
				JOptionPane.showMessageDialog(null, "wrong password");
			}
			*/
		}// end button if 
		
	}

it is really strange to me.... please help.

Recommended Answers

All 3 Replies

The first case:

if(loginF.getText()=="root" && passwordF.getText() == "root");

Here we have if statement with an empty statement as it's contained Statement. so we are not allowed to call else any more; and in all case the cod:

#
{
#
mb = new MenuBar(this);
#
mb.setBackground(java.awt.Color.white);
#
this.setJMenuBar(mb);
#
this.validate();
#
this.repaint();
#
setBounds(400, 400, 600, 500);
#
setVisible(true);
#
}

The second the block above doesn't excecute because the condition:

loginF.getText()=="root" && passwordF.getText() == "root"

because the condition doesn't stisfied.

Now tell me witch of the two if own the else clause?

//This code is from the java language specification 
if (door.isOpen())
if (resident.isVisible())
resident.greet("Hello!");
else door.bell.ring();

Hope it helps.

it was the variable type mismatch thing.. i m surprised that the compiler didn't catch it... still a bit confused but it is fixed.

if anyone is curious, here is how it is

login = loginF.getText();   //string declared as global
			pass = passwordF.getText(); //same
			
				
				if(login.equals("root") && pass.equals("root"))

it was the variable type mismatch thing.. i m surprised that the compiler didn't catch it... still a bit confused but it is fixed.

Thgere was nothing for the compiler to "catch". Your code is valid, but just didn't do what you wanted.
The equals(Object) method is defined on thge Object class, so any statement like

anyOldObject.equals(anyOtherOldObject)

is valid.
In your case it was

someJlabel.equals(someString)

which is obviously going to return false.

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.