Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JavaAdviser

Thread: [Help]Compare user input and data stored in *csv files

  1. #1
    Adr
    Adr is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Question [Help]Compare user input and data stored in *csv files

    Hey guys!
    I've been searching for almost a day and I can't seem to find a ready-made class for my problem.
    I am assigned to make a program that asks for a username and a password. Well if you don't have those you can just
    click the register button on the form and register for an account. The information given on the registration are then exported on the
    *csv file I have created. No problem with that, luckily.

    Now here comes the tricky part.
    When the user decides to login, he enters the username and password. And then the program should compare
    the input from the user with the data from the *csv file. I honestly don't know how to construct a code for that.
    Could you please help me?
    More power guys!

    BTW, here is my the code I have done for the meantime. This is for the register panel.
    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import javax.swing.*;
    import sun.security.util.Password;
    
    //ADRIAN NATABIO
    public class regForm extends javax.swing.JFrame {
    //    regForm r = new regForm();
        String regUsername;   
        String regPassword;
        char[] regPW;
        boolean b;
        
        public regForm() {
            initComponents();       
        }  
        public static String convertPw(char[] regPW)
        {
            String regPassword = "";
            for(int x=0; x<regPW.length; x++)
            {   
                regPassword += regPW[x];
            }
            return regPassword;
        }    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            regPwField = new javax.swing.JPasswordField();
            regUnField = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("[ADR] Register Panel");
    
            jLabel1.setText("Desired Username");
    
            jLabel2.setText("Desired Password");
    
            jButton1.setText("Register!");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(58, 58, 58)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(regPwField, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(regUnField, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(75, 75, 75)
                            .addComponent(jButton1)))
                    .addContainerGap(53, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(regUnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jLabel2)
                    .addGap(5, 5, 5)
                    .addComponent(regPwField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap(76, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            regUsername = regUnField.getText();   
            regPW = regPwField.getPassword();
            regPassword = convertPw(regPW);
            try
            {
                try (BufferedWriter out = new BufferedWriter(new FileWriter("c:\\test.csv", true))) 
                {               
                    out.write(regUsername);
                    out.write(",");
                    out.write(regPassword);
                    out.write('\n');
                    JOptionPane.showMessageDialog(rootPane, "Successfully registered!");
                    regPwField.setText("");
                    regUnField.setText("");
                }           
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(rootPane, "Error!");
                b = false;
            }      
            if(b==false)
            {            
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Successfully Registered!");
    //            r.setVisible(false);
            }      
        }                                        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(regForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(regForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(regForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(regForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new regForm().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPasswordField regPwField;
        private javax.swing.JTextField regUnField;
        // End of variables declaration
    }
    Last edited by Adr; 09-15-2012 at 04:26 PM.

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: [Help]Compare user input and data stored in *csv files

    You would have to read in the username/password-file again and then look for the repsective username/password-pair. If you find one, the login was successful, unsuccessful otherwise.
    Fubarable likes this.

  3. #3
    Adr
    Adr is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: [Help]Compare user input and data stored in *csv files

    Quote Originally Posted by JavaAdviser View Post
    You would have to read in the username/password-file again and then look for the repsective username/password-pair. If you find one, the login was successful, unsuccessful otherwise.
    Yes sir! I know that's the process.
    But the problem is I don't know how to construct the code for that.

  4. #4
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: [Help]Compare user input and data stored in *csv files

    Well, you already have the code to write to a file, so reading shouldn't be all that different.

    For inspiration you can look here Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O) or google for yourself.

Similar Threads

  1. text editor problem
    By danielparry in forum New To Java
    Replies: 3
    Last Post: 03-04-2011, 04:26 PM
  2. Help for a Text Editor.
    By necroninja in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2009, 06:21 PM
  3. SWT Text Editor Demo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:35 PM
  4. SWT Text Editor
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:43 PM
  5. Problem in Text Editor
    By mustahsan4u in forum New To Java
    Replies: 2
    Last Post: 03-26-2008, 03:34 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
  •