Reputation: 27
I have a GUI for user login.
First the user creates his account, and that information is stored in a .txt
file. I use PrintWriter
to append details in that file.
The details are stored using a separator. I can easily read every detail of a user from the file.
In the login UI, I have 2 JTtextField
components in a JFrame
the first one for the user name and the second onw for the password.
I get the values using the getText method:
String user = user.getText();
String password = password.getText();
I tried using BufferedReader
but I can't get it to work:
if(user.equals(br.readline))
What i want to do is scan the file and if anything in the file is equal to the username (The getText from user on Frame) then I want to use SetVisible to go to Next Frame
My problem is that even on wrong password and user it go to new frame
How can I fix that?
Code to Check for User and Password, found online on StackOverflow still now working.
Scanner sc = new Scanner(new File("Details.txt"));
while(sc.hasNextLine()) {
int val =0;
String line = sc.nextLine();
if(line.indexOf(user) !=-1 && line.indexOf(pass) !=-1) {
JOptionPane.showMessageDialog(null,"Login");
val = 1;
vf.setVisible(true);
break;
} else {
JOptionPane.showMessageDialog(null,"Invalid");
val = 0;
break;
}
}
Another Code which is used in for , while even in do while loop , still not working.
File file = new File("Details.txt");
BufferedReader br = new BufferedReader(new FileReader("Details.txt"));
String Line;
do{
if(user.equals(br.readLine()) && pass.equals(br.readLine())){
vf.setVisible(true);
} else {
JOptionPane.showMessageDialog(null,"Invalid");
}
}while((Line=br.readLine()) !=null )
This is how Users Details are Stored in My File.
=======================
=======================
First Name = Ahmed Ali
Last Name = Qazi
Address = Al-Abbass Colony pHase 2
Phone Number = +92032329301
Email Address = [email protected]
UserName = ahmedfirst67
Password = dangerd = 2hg
=======================
=======================
=======================
=======================
First Name = Ahm345
Last Name = Qa345
Address = Al-asfafs
Phone Number = +92032329301
Email Address = ahmgsdg
UserName = ahmegg
Password = dagg
Upvotes: 0
Views: 278
Reputation: 9756
It is not straightforward given your file structure but you can do it this way:
Here is the code:
private static boolean checkCredentials(String user, String pass) throws IOException {
Scanner sc = new Scanner(new File("Details.txt"));
boolean userFound = false;
String correctPassword = null;
String line;
while(sc.hasNextLine()) {
line = sc.nextLine();
// find the user
if(!userFound) {
userFound = line.contains("UserName = "+user);
} else {
// find the password
if(line.contains("Password = ")) {
correctPassword = line.substring(line.indexOf("=") + 2);
break;
}
}
}
return correctPassword!=null && correctPassword.equals(pass);
}
public static void main(String[] args) throws IOException {
System.out.println(checkCredentials("Bob", "dagg"));
System.out.println(checkCredentials("ahmegg", "daggy"));
System.out.println(checkCredentials("ahmegg", "dagg"));
}
This prints:
false
false
true
Stick it in your code like this:
val = 0;
String message = "Invalid";
if(checkCredentials(user, pass)) {
message = "Login";
val = 1;
vf.setVisible(true);
}
JOptionPane.showMessageDialog(null, message);
NOTE:
It would be lot easier if you stored the details on one line this way:
Ahmed Ali|Qazi|Al-Abbass Colony pHase 2|+92032329301|[email protected]|ahmedfirst67|dangerd = 2hg
Ahm345|Qa345|Al-asfafs|+92032329301|ahmgsdg|ahmegg|dagg
You could use the String.split
method to parse each line and get user and password at the same time.
NOTE 2:
Also it is very bad practice to store password unencrypted. If this is a school exercise it is fine, but if it is for a real life project, you might want to look at encrypting the passwords in the file.
Upvotes: 1