954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

something new (strange) to me

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.

k2k
Posting Whiz
352 posts since Nov 2007
Reputation Points: 15
Solved Threads: 1
 

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.

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

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"))
k2k
Posting Whiz
352 posts since Nov 2007
Reputation Points: 15
Solved Threads: 1
 
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 returnfalse.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You