I have txt file called samplename, in that file there are 3 lines values:
samplename
samplepassword
%%%%%%%%%%%%%%%%%
The Programm is about a login-register system. When I type
samplename
in the first JTextField and
samplepassword +
in the second JTextField and press the login button, it supposed to check if I have such account, if it has the right username&password. The Programm reads the things in, just it cant compare it to the the JTextFields. What am i doing Wrong?!?:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SomeClass
{
public JButton loginButton;
public JTextField login_username;
public JTextField login_password;
String check = null;
String check2 = null;
public static void main(String[] args)
{
SomeClass someClass = new SomeClass();
someClass.doSomething();
}
public void doSomething()
{
loginButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Scanner scanner = new Scanner(System.in);
BufferedReader rd = null;
String line;
boolean gothrew = false;
boolean ifDOES = false;
String Username;
String Password;
Password = login_username.getText().trim();
Username = login_password.getText().trim();
try
{
rd = new BufferedReader(new FileReader(
"C:\\users\\Dominik Gyarmati\\AppData\\Roaming\\KeepSafe\\users\\"
+ login_username.getText() + ".txt"));
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(null, "Your Account doesn't exist");
}
try
{
while ((line = rd.readLine()) != null)
{
if (check == null)
{
check = line;
check.trim();
gothrew = true;
System.out.println(check);
}
else if (gothrew == true)
{
check2 = line;
check2.trim();
System.out.println(check2);
gothrew = false;
ifDOES = true;
}
else if (check.equals(Username) && check2.equals(Password))
{
JOptionPane.showMessageDialog(null,
"You've logged into your account");
check = null;
check2 = null;
ifDOES = false;
}
else
{
JOptionPane.showMessageDialog(null,
"Username or Password is wrong");
check = null;
check2 = null;
ifDOES = false;
}
}
}
catch (IOException ioexex)
{}
}
});
}
}
EDIT: I just had to change
else if (check.equals(Username) && check2.equals(Password))
to
else if (check.equals(login_username.getText().trim()) &&
check2.equals(login_password.getText().trim())) {
First of all, your code smells bad:
Secondly, try to use debug tools: debugging perfectly answers to your question.
And finally, check this lines:
Password = login_username.getText().trim();
Username = login_password.getText().trim();
Replace Password
with Username
and vice versa.