• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to do a loop

 
Olliver Lim Kam Sian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There doesn't seem to be any code that looks at the userValid boolean and takes different actions depending on its value.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olliver Lim Kam Sian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic