Results 1 to 3 of 3
  1. #1
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default password/voting ID

    hello,

    i am writing a program where someone enters their "password" into a gui and then can vote for some people. So far the voting part is working but the password is not. For the password, the user enters any number(s) and the program should store it into an array. After that, they can vote. When someone votes again they enter a new password and that password is entered as the second term of the array. If you enter the same password then an error message should show up. Heres my code:

    Java Code:
    import javax.swing.*; // Required library
    import java.awt.*; // Required library
    import java.awt.event.*; // Required library
    import javax.swing.JOptionPane;
    public class HW9_2 extends JFrame {
      private JTextField tField = new JTextField("Enter Password"); // Create a text field
      private JLabel labelWithtField = new JLabel("");
      private JButton my1Button = new JButton("Login"); // Create a new button
      private JLabel label1WithText = new JLabel();
      private JButton my2Button = new JButton("Mickey Mouse"); // Create a new button
      private JLabel label2WithText = new JLabel();
      private JButton my3Button = new JButton("Donald Duck"); // Create a new button
      private JLabel label3WithText = new JLabel();
      private int status = 1;
      private double mm = 0;
      private double dd = 0;
      private int i = 0;
      private int c = 0;
    
      
      
    // Constructor
    public HW9_2() {
      setLayout(new GridLayout(2, 2, 0, 0)); // Define layout as a grid
      add(tField);
      add(labelWithtField);
    add(my1Button); // Make button appear in window
    add(label1WithText);
    add(my2Button); // Make button appear in window
    add(label2WithText);
    add(my3Button); // Make button appear in window
    add(label3WithText);
    
    
    my1Button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    double[] listlogin; // Declare the array
    listlogin = new double[100];
    //String tField = tField.getText(); // Get the text in the TextField
    String str = tField.getText();
    double login = Double.parseDouble(str);
    if (status == 1) {
      listlogin[c] = login;
      c++;
    }
    for (int i = 1; i < listlogin.length; i++) { // Process array with loop
      if (listlogin[c] == listlogin[i]) {
      JOptionPane.showMessageDialog(null, "ERROR");
    }
    }
    
    }});
    pretty much everything is ok except everything after the action listener

    thanks for your help

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) The array containing the passwords must not be created in the action listener because it handles multiple actions. It must be an instance variable of the class and the actionPerformed can check against it and add to it.
    2.) If the array of passwords is not persisted then running the program again will always use a new array of passwords since all the program's data will go away when the application is closed.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    U have created within the actionPerformed method.So,everytime the action is perfomed it will be re initialized.Create it globally and try to use.
    Ramya:cool:

Similar Threads

  1. Password asking
    By Kruptein in forum New To Java
    Replies: 23
    Last Post: 08-18-2009, 09:28 PM
  2. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  4. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM
  5. How to get password in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •