Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Password verification program

 
sunil choudhary
Ranch Hand
Posts: 143
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well,
Since a longtime I am trying to get my PAssword verification program work but it always fails the check
I am wondering what are other ways to compare the user entered string to the original string.
here is the program I am using
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//import java.io.*;
/*<applet<br /> code=Login.class<br /> width=800<br /> height=150>
</applet>*/
public class Login extends Applet implements ActionListener {
String s;
TextField usernm,passwd;
Button enter;
public void init(){

usernm= new TextField(80);
add(usernm);
passwd= new TextField(80);
add(passwd);
enter= new Button("Enter");
add(enter);
enter.addActionListener(this);
}


public void actionPerformed(ActionEvent event){

if (event.getSource()==enter)
{
//now compare this username agaisnt the list of users and their passwords
String msg="hehe";
s=String(passwd);
if ((s.equals(msg)))
{
System.out.println("Entered");//have t introduce a link to another window:like a triggerin VB

}
else
{System.out.println("Caught Redhanded"+msg);

}
}
}
}
please help !!!
 
Pratap Reddy
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using s=String(passwd); use s = passwd.getText();
Then it should work fine.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this question has nothing to do with certification, I am moving this thread to
Java in General(intermediate)
Ajith
 
sunil choudhary
Ranch Hand
Posts: 143
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pratap and others
,please take a look at following code also
I am Modifying the program a bit to accomaodate the PassWordField
here the program is giving an error as undefined variable or a class name s1 however s1 is completely defined as a String object
then how to debug the code I know that the o/p of a password field can be retrieved by getPassword method but this returns the value in char.then a new probelem has crept up as to how to compare a char with a String that is entered in the password field
please help!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Login3
{ public static void main(String args[])
{
JFrame frame=new EntryScreen();
frame.show();
}
}
class EntryScreen extends JFrame implements ActionListener
{
public EntryScreen()
{
setSize(300,200);
setTitle("Login");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e) //is initialising
{
System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout()); ///vvvvip line
JPanel canvas=new JPanel();
contentPane.add(canvas,"Center");
JPanel p=new JPanel();
JTextField name=new JTextField(15);
contentPane.add(name,"Center");
JPasswordField pass=new JPasswordField(15);
contentPane.add(pass);
JButton submit=new JButton("Submit");
submit.addActionListener(this);
contentPane.add(submit);

}//entry screen
public void actionPerformed(ActionEvent ae)
{
//String s1=pass.getText();
// String t=new String("hehe");
if(s1.equals(t))
{ System.out.println("INSide");
}
else
{System.out.println("nothing");
}

}}
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem now is that the JPasswordField() variable 'pass' is out of scope. If you uncomment the two lines in your actionedPerformed method and then declare the pass variable in the top of your EntryScreen class (making it an instance variable), it should work. Something like this

I hope this helps.
Sean
 
sunil choudhary
Ranch Hand
Posts: 143
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sean,for giving a try It feels good to have you by my side.
I tried your method but strangly it is giving error as undefined variable or classname pass
Well,getText method is also deprecated for JPasswordField whic is the warning issued by the javac command
I am posting my findings about it,See if it gives you an Idea
JPasswordField
getPassword
public char[] getPassword()
Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException. For stronger security, it is recommended that the returned character array be cleared after use by setting each character to zero.
Can any one give an example of using this
JPasswordField pass=new JPasswordField(15);
contentPane.add(pass);
...
....
public void actionPerformed(ActionEvent ae)
{
String s1=pass.getPassword();
String t=new String("hehe");
if(s1.equals(t))
{ System.out.println("INSide");
-------
}
else
{System.out.println("nothing");
-----
}
complete code below //

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Login3
{ public static void main(String args[])
{
JFrame frame=new EntryScreen();
frame.show();
}
}
class EntryScreen extends JFrame implements ActionListener
{
public EntryScreen()
{
setSize(300,200);
setTitle("Login");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e) //is initialising
{
System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout()); ///vvvvip line
JPanel canvas=new JPanel();
contentPane.add(canvas,"Center");
JPanel p=new JPanel();
JTextField name=new JTextField(15);
contentPane.add(name,"Center");
JPasswordField pass=new JPasswordField(15);
contentPane.add(pass);
JButton submit=new JButton("Submit");
submit.addActionListener(this);
contentPane.add(submit);

}//entry screen
public void actionPerformed(ActionEvent ae)
{
String s1=pass.getPassword();
String t=new String("hehe");
if(s1.equals(t))
{ System.out.println("INSide");
}
else
{System.out.println("nothing");
}

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