I am making a log in gui that read from 2 text file. The username line matches the same line as in password. If username is in line 3 then password will be in line 3 in password.txt If the username and password matches from the username.txt and password.txt, the system will log you in. I am able to get the specific line number from the username text but I keep getting null when password.txt is read.
I tried using scanner but i dont need to read every line. i just need to read the specific line.
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
try {
File f1=new File("username.txt");
File f2=new File ("password.txt");
String[] words=null;
FileReader fr1 = new FileReader(f1);
FileReader fr2 = new FileReader(f2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String s;
String user = username.getText();
String pass=String.valueOf(password.getPassword());
String usertxt = " ";
String passtxt=" ";
int count =0;
while((s=br1.readLine())!=null) //Reading Content from the file
{
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equals(user)) //Search for the given word
{
System.out.print(count);
for (int i = 0; i < count; i++)
{
br2.readLine();
passtxt = br2.readLine();
}
System.out.print(passtxt);
if(pass.equals(passtxt))
JOptionPane.showMessageDialog(null,"User has logged in");
}
}
count++;
}
}
catch (IOException e)
{
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}
}
I want the string pass to equal passtxt.