my dog learned polymorphism*
The moose likes Beginning Java and the fly likes trying to do a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trying to do a loop" Watch "trying to do a loop" New topic
Author

trying to do a loop

Olliver Lim Kam Sian
Greenhorn

Joined: Sep 26, 2007
Posts: 12
Hello everyone!
I am currently working on a login code,i've put it in another program and the login is operational.But the thing is when i type in a wrong password,i get a message that tells me invalid password but logs in instead of making me try again, I've tried to put a do while loop with it but ending up with errors.
Can anyone help me? here is the code MyLogin in the username and password constructor that i call i n this one!

private boolean login() {

boolean userValid = false;
MyLogin Login = new MyLogin (new Frame("")); //Referencing MyLogin
requestFocus();
if (Login.id) {
username = Login.username.getText();
password = Login.password.getText();
userValid = validateUser(username , password);
// System.out.println
//("The password for " + username
// + " is " + (userValid?"valid":"invalid"));
JOptionPane.showMessageDialog(new JFrame(), "The password is invalid for " + username );
}

else
System.out.println
("Cancel was pressed.");

Login.dispose();
return userValid;

}

private boolean validateUser(String usr, String pwd) {
username = "Olliver";
password = "Lim";
return (usr.equals(username) && pwd.equals(password));

}

********************************************************
Here is what I have been trying to do


//start login method
private boolean login() {
do{
boolean userValid = false;
MyLogin Login = new MyLogin (new Frame("")); //Referencing MyLogin
requestFocus();
if (Login.id) {
username = Login.username.getText();
password = Login.password.getText();
userValid = validateUser(username , password);
// System.out.println
//("The password for " + username
// + " is " + (userValid?"valid":"invalid"));
JOptionPane.showMessageDialog(new JFrame(), "The password is invalid for " + username );
}

else
System.out.println
("Cancel was pressed.");

Login.dispose();
//return userValid;
}while( equals(username)&& equals(password));
return isValid();
}


private boolean validateUser(String usr, String pwd) {
username = "Olliver";
password = "Lim";
return (usr.equals(username) && pwd.equals(password));

}


Hope someone can help me!!
[ October 29, 2007: Message edited by: Olliver Lim Kam Sian ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41117
    
  45
There doesn't seem to be any code that looks at the userValid boolean and takes different actions depending on its value.


Ping & DNS - my free Android networking tools app
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

a quick tip for getting help:

If you are getting errors, it helps if you tell us exactly what kind of errors they are. Compile errors? Run time errors? Cut-n-pastes of the exact error message you get goes a LONG way in helping people figure out the problem.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Olliver Lim Kam Sian
Greenhorn

Joined: Sep 26, 2007
Posts: 12
im not getting errors anymore but i can't get the code to reject a wrong password or username!here is wat i've done!

private boolean login() {
do{
boolean userValid = true;
MyLogin Login = new MyLogin (new Frame("")); //Referencing MyLogin
requestFocus();
if (Login.id) {
username = Login.username.getText();
password = Login.password.getText();
userValid = validateUser(username , password);
System.out.println
("The password for " + username
+ " is " + (userValid?"valid":"invalid"));
}



else
System.out.println
("Cancel was pressed.");

Login.dispose();

}while( username.equals(username) && username.equals(password));
return isValid();
}

private boolean validateUser(String usr, String pwd) {
username = "Olliver";
password = "Lim";
return (usr.equals(username) && pwd.equals(password));

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41117
    
  45
i can't get the code to reject a wrong password or username


What does this mean? Does the System.out.println statement show "valid" for a failed login and vice-versa, or what is happening?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's a lot of unknowns there for somebody who doesn't have a copy of the rest of your code. So can you tell us what that code does? And tell us what you want it to do, too. That's another unknown.
Olliver Lim Kam Sian
Greenhorn

Joined: Sep 26, 2007
Posts: 12
This is the seperate MyLogin.java file,I use the login.java in a client code that is connected to a server to call for MyLogin.java. sincerely I don't know much about the code, butI tried to get it to work properly(i.e enable login when username&password is ok and ask to retype if username or password is wrong)
But failed thats why i am turning to you guys!

Thanks for considering my problem

import java.awt.*;
import java.awt.event.*;

public class MyLogin extends Dialog implements ActionListener {
boolean id = false;
Button ok,can;
TextField username;
TextField password;


MyLogin(Frame frame){
super(frame, "Welcome", true);
setLayout(new FlowLayout());
username = new TextField(15);
password = new TextField(15);
password.setEchoChar('*');
add(new Label("User :"));
add(username);
add(new Label("Password :"));
add(password);
addOKCancelPanel();
createFrame();
pack();
setVisible(true);
}

void addOKCancelPanel() {
Panel p = new Panel();
p.setLayout(new FlowLayout());
createButtons( p );
add( p );
}

void createButtons(Panel p) {
p.add(ok = new Button("OK"));
ok.addActionListener(this);
p.add(can = new Button("Cancel"));
can.addActionListener(this);
}

void createFrame() {
Dimension d = getToolkit().getScreenSize();
setLocation(d.width/4,d.height/3);
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource() == ok) {
id = true;
setVisible(false);
}
else if(ae.getSource() == can) {
id = false;
setVisible(false);
}

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to do a loop
 
Similar Threads
not getting the session value on page..!!!
LoginPanel is not abstract and does not override abstract method actionPerformed..
java applet redirect fails
Plz help to add new window on current one!
error in compling --can anyone help