CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    81

    Applet logic error

    I am having a terrible time figuring out my logic error. The program is supposed to test if the login info matches what was entered... but for some reason, no matter what I enter it says it's successful... Help!

    Code:
    /*
    	Chapter 5:	Programming Assignment 4
    	Programmer:	Ben B*******
    	Date:		04/22/11
    	Filename:	PasswordApplet.java
    
    */
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class PasswordApplet extends Applet implements ActionListener
    {
       //Declaring variables
       String id, password;
       boolean success;
       String idArray[] = {"id1", "id2"};
       String passwordArray[] = {"11111111", "11111111"};
    
       //Create components for applet
       Label nameLabel = new Label("Ben Brotsker    04/22/11\n\n");
       Label headerLabel = new Label("Please type your ID and Password");
    
       Label idLabel = new Label("ID:");
          TextField idField = new TextField(8);
    
       Label passwordLabel = new Label("Password:");
          TextField passwordField = new TextField(8);
    
    
       Button loginButton = new Button("Login");
    
       public void init()
       {
          //Set color, layout, and add components
          setBackground(Color.orange);
    
          setLayout(new FlowLayout(FlowLayout.LEFT,50,30));
    
    	  add(nameLabel);
          add(headerLabel);
    
          add(idLabel);
             add(idField);
             idField.requestFocus();
    
          add(passwordLabel);
             add(passwordField);
     		 passwordField.setEchoChar('*');
    
          add(loginButton);
             loginButton.addActionListener(this);
       }
    
       public void actionPerformed(ActionEvent e)
       {
          success = false;
    
          //Sequential search
    		for(int i=0;i<idArray.length;i++)
    		{
    			id = idField.getText();
    			password = passwordField.getText();
    			if ((idArray[i].compareTo(id)==0) &&
    			(passwordArray[i].compareTo(password)==0))
    				success=true;
    		}
    		if (success=true)
    			headerLabel.setText("Login successful");
    		else
    		{
    			headerLabel.setText("Unsuccessful.  Try Again");
    			clearFields();
    		}
       }
       void clearFields()
       	{
    		idField.setText("");
    		passwordField.setText("");
    		idField.requestFocus();
    	}
    }

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Applet logic error

    Code:
    if (success=true)
    That's why it is not only redundant but a very bad practice to compare a boolean to true (or false). I guess you intended to write
    Code:
    if (success==true)
    but what you have there is an assignment, so instead of checking if success is true you are making it true. Use this instead:
    Code:
    if (success)
    Also, shouldn't there be a break after you change success to true in the loop?

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: Applet logic error

    And of course if you ARE going to use the terrible practice of comparing variables like that, or if you are comparing int's maybe, you should always place the variable on the right hand side so that you don't do what you did.

    if(true == success)

    This will throw an error if you only place one equals sign in there.

    if(5 == x)

    Things like that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured