|
-
April 22nd, 2011, 02:12 PM
#1
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();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|