how to check the password?
Hi, i try to validate the username and the password that i get using a gui menu in a another class. But then it cannot validate the password and it keep on looping to check the username......
my txt file look like this
john;lim;male;12;3;1964;12345;ang mo kio;abc;abc
mary;smith;female;3;11;1950;45678;sengkang;def;def
please tell me the mistakes.... thank u
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
publicclass CustomerDB
{
private String file;
private String delimiter;
private ArrayList customerList;
public CustomerDB(String file, String delimiter)
{
this.file = file;
this.delimiter = delimiter;
try{
loadCustomerData();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(),"Fatal Error", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
}
privatevoid loadCustomerData()throws Exception{
customerList =new ArrayList();
String record;
StringTokenizer tokenizer;
String firstName, lastName, gender, birthDate, birthMonth, birthYear, telephone, address;
char [] password =newchar[10];
char [] confirmPass =newchar[10];
try{
BufferedReader reader =new BufferedReader(new FileReader(file));
record = reader.readLine();
while (record !=null){
tokenizer =new StringTokenizer(record,delimiter);
firstName = tokenizer.nextToken();
lastName = tokenizer.nextToken();
gender = tokenizer.nextToken();
birthDate = tokenizer.nextToken();
birthMonth = tokenizer.nextToken();
birthYear = tokenizer.nextToken();
telephone = tokenizer.nextToken();
address = tokenizer.nextToken();
String password1 = tokenizer.nextToken();
for (int i = 0; i < password1.length(); i++){// putting the password into array of char
password[i] = password1.charAt(i);
}
String confirmPass1 = tokenizer.nextToken();
for (int i = 0; i < confirmPass1.length(); i++){
confirmPass[i] = confirmPass1.charAt(i);
}
customerList.add(new Customer(firstName, lastName, gender, birthDate,
birthMonth, birthYear, telephone, address, password, confirmPass));
record = reader.readLine();
}
reader.close();
}
catch (FileNotFoundException e){
Exception error =new Exception(file +" not found");
throw error;
}
catch (IOException e){
Exception error =new Exception("Error reading " + file);
throw error;
}
catch (NoSuchElementException e){
Exception error =new Exception("Wrong format of " + file);
throw error;
}
}
public ArrayList getCustomerList(){
return customerList;
}
publicboolean validate(String firstName,char [] password){
boolean result =false;
boolean isCorrect =true;
Customer f;
int i = 0;
String inPass =new String (password);
while (result !=true){
f = (Customer) customerList.get(i);
if((f.getFirstName()).equals(firstName)){
System.out.println(f.getFirstName());
JOptionPane.showMessageDialog(null,"Correct ID","Validation", JOptionPane.ERROR_MESSAGE);
//NEED TO CONVERT THE GETPASSWORD TO CHAR
char [] correctPassword = f.getPassword();
System.out.println("After char[]correctPass" + correctPassword);
if(password.length != correctPassword.length){
isCorrect =false;
break;
//System.out.println("password length" + password.length + correctPassword.length + isCorrect);
}
else{
for(int j = 0; j < password.length; j++){
System.out.println("password length 2");
if( password[j] != correctPassword[j] ){
System.out.println("password length 3");
isCorrect =false;
break;
}
}
}
//Zero out the password.
for(int j = 0; j < correctPassword.length; j++ ){
correctPassword[j] = 0;
}
if (isCorrect){
result =true;
JOptionPane.showMessageDialog(null,"Correct id","Validation", JOptionPane.ERROR_MESSAGE);
break;
}
}
i++;
}
return result;
}
publicstaticvoid main(String [] args)
{
CustomerDB f =new CustomerDB("customerData.txt",";");
ArrayList clist = f.getCustomerList();
for (int i = 0; i < clist.size(); i++)
System.out.println((Customer) clist.get(i));
}
}

