I can't figure out what is the problem, I need you pros to help me with this please. I need it to login by reading their username and password in text file, and load those user information into the text field that store in the text file too. For now my code problem is when I click on Login nothing happen.
Here's my text file information :
jack123
jackabc
jc@gmail.com
Jack
123123123
012344567
no1,taman abc.
And here is my Login code :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EventObject;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class LoginButton {
@FXML
private TextField txtfUsername;
@FXML
private TextField txtfPassword;
@FXML
private Button btnLogin;
@FXML
private Button btnRegister;
@FXML
void setOnLogin(ActionEvent event1) throws IOException{
String username = txtfUsername.getText();
String password = txtfPassword.getText();
boolean grantAccess = false;
File f = new File("D:\\Apps\\Esclipse\\Assignment\\src\\application\\UserData.txt");
try {
Scanner read = new Scanner(f);
int noOfLines=0;
while(read.hasNextLine()){
noOfLines++;
}
for(int i=0; i<noOfLines; i++){
if(read.nextLine().equals(username)){
i++;
if(read.nextLine().equals(password)){
grantAccess=true;
read.close();
break;
}
}
}
if(grantAccess){
Parent root1 = FXMLLoader.load(getClass().getResource("UserGUI.fxml"));
Scene scene1 = new Scene(root1, 750, 600);
Stage stage1 = (Stage) ((Node) event1.getSource()).getScene().getWindow();
stage1.setTitle(" Register ");
stage1.setScene(scene1);
stage1.show();
}
else{
System.exit(0);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@FXML
private void setOnRegister(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
Scene scene = new Scene(root, 750, 600);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setTitle(" Register ");
stage.setScene(scene);
stage.show();
}
}
Register code :
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class RegisterButton {
public String Email, Username, Password, Name, IC, Phone, Address;
@FXML
private TextField txtfEmailR, txtfUsernameR, txtfPasswordR ,txtfNameR, txtfICR, txtfPhoneR;
@FXML
private TextArea txtaAddressR;
@FXML
private Button btnRegisterR;
@FXML
private Button btnExit;
@FXML
private Button btnBack;
@FXML
private void OnBack(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LoginGUI.fxml"));
Scene scene = new Scene(root, 750, 600);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setTitle(" Welcome ");
stage.setScene(scene);
stage.show();
}
@FXML
void OnExit(ActionEvent event) {
Platform.exit();
}
@FXML
void OnRegister(ActionEvent e) {
String line = txtfEmailR.getText() +txtfUsernameR.getText() +txtfPasswordR.getText() +txtfNameR.getText() +txtfICR.getText() +txtfPhoneR.getText() +txtaAddressR.getText() +"\n";
FileWriter file_writer;
try {
file_writer = new FileWriter("D:\\Apps\\Esclipse\\Assignment\\src\\application\\UserData");
BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
buffered_Writer.write(line);
buffered_Writer.flush();
buffered_Writer.close();
System.out.println("Registed.");
System.exit(0);
}catch (IOException E) {
System.out.print("Error is ");
}
}
}
I am guessing you are in an infinite while loop since you are calling read.hasNextLine(), but you aren't actually reading the line, so you're stuck at the first line. You will probably have to put a readLine() in your while loop and then go back to the beginning again to parse the lines.
Edit:
After reading your code, I don't really see a reason for you to have the number of lines, so you could do everything in your first while loop like this:
Scanner read = new Scanner(f);
//int noOfLines=0;
while(read.hasNextLine()){
if(read.nextLine().equals(username)){
if(read.nextLine().equals(password)){
grantAccess=true;
read.close();
break;
}
}
}
but if you need to know the number of lines for some reason, you could do it like this:
Scanner read = new Scanner(f);
int noOfLines=0;
while(read.hasNextLine()){
read.nextLine();
noOfLines++;
}
read.close();
read = new Scanner(f);
for(int i=0; i<noOfLines; i++){
if(read.nextLine().equals(username)){
i++;
if(read.nextLine().equals(password)){
grantAccess=true;
read.close();
break;
}
}
}
Also, a potential issue I can see with your code is you might get incorrect results if someone's password is someone else's username.