[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.
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
}
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.
Re: [Help]Compare user input and data stored in *csv files
Quote:
Originally Posted by
JavaAdviser
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.
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.