need help!, i have an assignment i need to make an login txt based, my program only read the first line
the txt look like this
Luthfi Luthfi Fitra
Fitra Fitra Khori
Khori Fitra Syifa
but it only read the first, so i could log in only using the top account here is my code
public Login() {
initComponents();
txtPass.setText("");
}
public void Masuk(){
try {
String lokasi = "D:/settings.txt";
String username = txtUser.getText();
String password = txtPass.getText();
FileReader fr = new FileReader(lokasi);
BufferedReader br = new BufferedReader(fr);
String line,user,pass;
boolean isLoginSuccess = false;
while ((line = br.readLine()) != null) {
user = line.split(" ")[1].toLowerCase();
pass = line.split(" ")[2].toLowerCase();
if (user.equals(username) && pass.equals(password)){
isLoginSuccess = true;
this.dispose();
MainMenu mm = new MainMenu();
mm.setLocationRelativeTo(null);
mm.setVisible(true);
break;
}
else{
JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD SALAH","WARNING!!",JOptionPane.WARNING_MESSAGE);
break;
}
}
fr.close();
}catch(Exception e){
e.printStackTrace();
}
}
also why is everytime i insert the correct username and id it shows me that im succses but it show me the USERNAME/PASSWORD IS WRONG too
You have problem in getting substring from file, change code like following snipet
user = line.substring(0, 6).toLowerCase();
pass = line.substring(7, 12).toLowerCase();
Update: Based on your comment , i am changing my solution
1) Change your file format like this
Luthfi Luthfi Hehe
Fitra Luthfi Khori
Syifa Khori Luthfi
Khori Syifa Luthfi
here first word in each line is user name and second word is password
2) Change your code like following
user = line.split(" ")[1].toLowerCase();
pass = line.split(" ")[2].toLowerCase();
3) if any username and password match, make login and break from while loop
Update again
package problems;
import java.io.BufferedReader;
import java.io.FileReader;
public class FileRd {
public static void main(String args[]) {
try {
String lokasi = "D:/settings.txt";
String username = txtUser.getText();
String password = txtPass.getText();
FileReader fr = new FileReader(lokasi);
BufferedReader br = new BufferedReader(fr);
String line, user, pass;
boolean isLoginSuccess = false;
while ((line = br.readLine()) != null) {
user = line.split(" ")[1].toLowerCase();
pass = line.split(" ")[2].toLowerCase();
if (user.equals(username) && pass.equals(password)) {
isLoginSuccess = true;
this.dispose();
MainMenu mm = new MainMenu();
mm.setLocationRelativeTo(null);
mm.setVisible(true);
break;
}
}
if (!isLoginSuccess) {
JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}