Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the coad is give as follow
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class lg extends Frame implements ActionListener
{
JLabel l1,l2,l3;
JTextField t;
JPasswordField p;
JButton b;
lg()
{
l1=new JLabel("user id");l1.setBounds(50,100,100,50);
t=new JTextField();t.setBounds(100,100,100,50);
l2=new JLabel("pass. wd");l2.setBounds(50,180,100,50);
p=new JPasswordField();p.setBounds(100,180,100,50);
b=new JButton("login");b.setBounds(150,250,100,50);b.setBackground(Color.green);
l3=new JLabel();l3.setBounds(150,330,200,50);
b.addActionListener(this);
add(l1);add(l2);add(t);add(p);add(b);add(l3);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String u=t.getText();
char c[]=p.getPassword();
String w=String.valueOf(c);
int i=Integer.parseInt(w);
System.out.println(p);
if(e.getSource()==b)
{
String v=new String("snks");
if(u==v&&i==1234)
System.out.println("match");
else
System.out.println("not match");
}
}
public static  void main(String...s)
{
new lg();
}
}


What I have tried:

the password match but user id not match it throws run time error
Posted
Updated 19-Aug-18 12:09pm
Comments
Patrice T 19-Aug-18 13:52pm    
What error message and position ?

1 solution

when you ask a question related to error, please also post the error.

if w is not number, contains space or other char parseInt will throw exception. wrap that call with try catch. you still have issue with validating user.

try
Java
if(u.compareTo(v) == 0) { // u==v will not work, in practical cases. Since == will compare object
  ....
}


To assign literal string you can simply write
Java
v = "your desired constant";


Please examine this code on your spare time you will find something interesting.
Java
public static void main(String[] args) throws IOException, InterruptedException {
  String x="000";
  String y = new String("000");
  String z = "000";
  System.out.println(x==y); // false
  System.out.println(x==z); // true
  System.out.println(x.compareTo(y)==0); // true
  System.out.println(x.equals(y)); // true
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900