IF STATEMMENT is not working completely

I can not get my if statement to complete the false condition and it should display "Login Successful" if it matches an item in the array and should clear the fields and display "Invalid. Try Again" if not successful.

Can someone help me with this?

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class PasswordApplet extends Applet implements ActionListener

{

//Declaring variables

String id;

String password;

boolean success;

//Construct variables

String idArray[] = {"tuetken", "jones", "smith", "white", "black", "johnson"};

String passwordArray[] = {"redfox", "reddog", "bravo", "alpha", "tango",};

//Create components for applet

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.blue);

setLayout(new FlowLayout(FlowLayout.LEFT,50,30));

add(headerLabel);

//headerLabel.setText("Login Successful");

add(idLabel);

add(idField);

idField.requestFocus();

add(passwordLabel);

add(passwordField);

passwordField.setEchoChar('*');

add(loginButton);

loginButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

id = idField.getText();//reads the text

password = passwordField.getText();//reads the text

success = true;

//Sequential search

for (int i = 0; i < idArray.length; i++)

if((idArray.compareTo(id)==0) &&

(passwordArray.compareTo(password)==0))

//success = true;

{

if(success == true){

headerLabel.setText("Login Successful");

repaint();

}else if(success == false){

headerLabel.setText("Invalid. Try Again.");

repaint();}

idField = null;

passwordField = null;

idField.requestFocus();

}

}

}

[2232 byte] By [amdooda] at [2007-11-26 21:11:10]
# 1
If you posted the code inside code tags with proper indentation it would be a lot easier to see what was inside what loop. As it is, your code is just unreadable. See the "Formatting tips" link above the box you post in and try again.
DrClapa at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 2
u commented out success = true...use teh code tags
Futurisdom_Developera at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 3

Is this what you were asking me to do?

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class PasswordApplet extends Applet implements ActionListener

{

//Declaring variables

String id;

String password;

boolean success;

//Construct variables

String idArray[] = {"tuetken", "jones", "smith", "white", "black", "johnson"};

String passwordArray[] = {"redfox", "reddog", "bravo", "alpha", "tango",};

//Create components for applet

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.blue);

setLayout(new FlowLayout(FlowLayout.LEFT,50,30));

add(headerLabel);

//headerLabel.setText("Login Successful");

add(idLabel);

add(idField);

idField.requestFocus();

add(passwordLabel);

add(passwordField);

passwordField.setEchoChar('*');

add(loginButton);

loginButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

id = idField.getText();//reads the text

password = passwordField.getText();//reads the text

success = true;

//Sequential search

for (int i = 0; i < idArray.length; i++)

if((idArray[i].compareTo(id)==0) &&

(passwordArray[i].compareTo(password)==0))

success = true;

{

if(success == true){

headerLabel.setText("Login Successful");

repaint();

}else if(success == false){

headerLabel.setText("Invalid. Try Again.");

repaint();}

idField = null;

passwordField = null;

idField.requestFocus();

}

}

}

amdooda at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 4
success is ALWAYS true, no matter what the values of id and password are. It's initial value is true and if the password matches the id's password it's also true. Initialize it to false and then it will work ;)
benubacha at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 5

Do this :

public void actionPerformed(ActionEvent e)

{

id = idField.getText();//reads the text

password = passwordField.getText();//reads the text

success = false; //assign false value here to success

//Sequential search

for (int i = 0; i < idArray.length; i++)

if((idArray[i].compareTo(id)==0) &&

(passwordArray[i].compareTo(password)==0))

success = true;

{

if(success == true){

headerLabel.setText("Login Successful");

repaint();

}else if(success == false){

headerLabel.setText("Invalid. Try Again.");

repaint();}

idField = null;

passwordField = null;

idField.requestFocus();

}

}

}

PurpleSkiesa at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 6
You could at least say "thanks guys", not to mention spoiling us with some dukes ;-)
benubacha at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 7
I'm a newbie first off and I just starting learning this area (counter controlled loops) today. Why does this not need a main()? Also it's not clearing the incorrect login info? I see where it says idField = null, etx so why it's it clearing?
dbrinea at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 8

The lack of main() has nothing to do with counter controlled loops: it's because the OP was writing an applet.

Applets are described in Sun's Tutorial here: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html They don't have a main() becuase they "exist" in an environment that has already "started up" (a browser or whatever). On the other hand an applet must cope with the user going to another web page and then returning. (Should it stop? pause in some way? keep going?) so different methods - a bit like main() - have to be able to deal with such events.

If you're new to programming stick to your loops! At least in practice although doesn't hurt to read around a bit.

pbrockway2a at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 9
Thanks for the fast response. The control loops are definitely complicated to the new guy or gal. I do have another question . It's not clearing the incorrect login info? I see where it says idField = null, etx so why it's it clearing?
dbrinea at 2007-7-10 2:48:33 > top of Java-index,Java Essentials,Java Programming...
# 10

> It's not clearing the incorrect login info? I see where it says idField = null, etx so

> why it's it clearing?

The expression idField=null sets the value of the idField variable. It does not clear anything on the screen. The correct way of doing that would be:idField.setText("");

Once it has been placed within its container the TextField has a sort of "life of its own": it goes on responding to user input and displaying that input on the screen whatever later happens to the value of the idField variable.

The code that said "idField=null" can only lead to problems the next time actionPerformed() is invoked. The very first line says "id = idField.getText()" and if idField is null that will lead to a NullPointerException. In fact if that code is followed there will be no way at all of accessing the text in the text field.* The text is still there - it displays on the screen - but the value of idField has been altered so that it no longer refers to the text field.

* Or, at any rate, no straightforward way.

pbrockway2a at 2007-7-10 2:48:34 > top of Java-index,Java Essentials,Java Programming...
# 11

1. while the counter loops are mind blowing, atleadt to me, can this problem be coded using while loops instead?

2. What is recommended for the beginner problem as far as learning and code "assignment" if you will? I understand I'm going to write some enormous coded application but I really want to learn the "basics" and progress nicely. I know how to have items prints on the screen using System.out, etc. Some folks may just want the answer to problems. I want to learn how and why something is , how flow works and why, etc.

thank for all the feedback. I really do appreciate it. Folks like yourself is why people like keep trying to learn and be productive.

dbrinea at 2007-7-10 2:48:34 > top of Java-index,Java Essentials,Java Programming...
# 12

I haven't read all the applet code that started this all that closely (and bear in mind it was wrong to start with and various people have made various suggestions of various quality). But ...

> 1. while the counter loops are mind blowing, atleadt to me, can this problem be

> coded using while loops instead?

In this case a for loop looks appropriate. The strategy is:

(1) Loop through the id array until you find the user

(2) When you do check the password against the coresponding entry in the password array

(3a) If all of this succeeds set the success variable to "true"

(3b) If it fails for any reason (user not there, or password wrong) clear the text fields and leave the success variable as "false".

> 2. What is recommended for the beginner problem as far as learning and

> code "assignment" if you will?

I reccommend Sun's Tutorial: http://java.sun.com/docs/books/tutorial/index.html Or search his forum for other suggestions.

pbrockway2a at 2007-7-10 2:48:34 > top of Java-index,Java Essentials,Java Programming...
# 13
thank you
dbrinea at 2007-7-10 2:48:34 > top of Java-index,Java Essentials,Java Programming...