Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do get JavaFX to read a text file? I want to be able to enter a name and pas

ID: 3859960 • Letter: H

Question

How do get JavaFX to read a text file? I want to be able to enter a name and password and JavaFX to display the account details, thanks in advance.

The files are in a .txt file and look like this:

Customer.txt (First 2 are ID & Password)

"3001":"1234":"Billy":"Carter":"Georgia":"bc@yahoo.com"
"3002":"1111":"Tony":"Danza":"Atlanta":"ggggg@hotmail.com"
"3003":"6789":"Terry":"March":"Texas":"mm@ymail.com"
"3004":"1111":"Susan":"Slater":"Arizona":"ss@yahoo.com"
"3005":"1212":"Tony":"Randal":"California":"tr1@hotmail.com"
"3006":"9999":"Bill":"Gates":"Washington":"bgates@ms.com"

Accounts.txt (Account#: Customer ID: Account Type: Balance)

"90000":"3003":"SAV":$8855.90
"90001":"3003":"CHK":$786.54
"90002":"3001":"SAV":$9654.13
"90003":"3006":"SAV":$17619.34
"90004":"3006":"CHK":$100.29
"90005":"3006":"MMA":$700356.23
"90006":"3001":"MMA":$23479.10
"90007":"3004":"MMA":$18000.00
"90008":"3005":"MMA":$25000.00
"90009":"3006":"SAV":$9000.00
"90010":"3003":"MMA":$10000.00

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.FlowPane;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

import javafx.event.Event;

import javafx.event.EventHandler;

public class AccountLogin extends Application implements EventHandler<ActionEvent>{

Button btn = new Button("Login");

Button mbtn = new Button("Exit");

TextField fntf = new TextField();

TextField lntf = new TextField();

TextField pwtf = new TextField();

public void start(Stage primaryStage) {

btn.setOnAction(this);

GridPane root = new GridPane();

root.add(new Label("First Name: "),0,0);

root.add(fntf,1,0);

root.add(new Label("Last Name: "),0,1);

root.add(lntf,1,1);

root.add(new Label("PW: "),0,2);

root.add(pwtf,1,2);

root.add(btn,0,3);

root.add(mbtn,1,3);

btn.setOnAction(this);

mbtn.setOnAction(this);

Scene scene = new Scene(root, 200, 150);

  

primaryStage.setTitle("ChattBank Login");

primaryStage.setScene(scene);

primaryStage.show();

}

public void handle(ActionEvent event) {

if(event.getSource() == btn) {   

System.out.println("Login Btn Pressed.....");

CustomerGUI2 cg = new CustomerGUI2();

}

if(event.getSource() == mbtn) {

System.out.println("Exit Btn Pressed.....");

System.exit(0);

}   

}

  

public static void main(String[] args) {

launch(args);

}//end main()

}//end class

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.File;

import java.io.IOException;

import java.net.URISyntaxException;

import java.net.URL;

import java.util.Scanner;

import java.util.StringTokenizer;

public class Customer2 extends Person2{

//Properties

   public int password;

   public int customerID;

   public AccountList2 list;

//Constructors

   public Customer2(){

      super();

      list = new AccountList2();

      password = 0;

      customerID = 0;

   }

   public Customer2(int pw, int cid ,String fn, String ln, String addr, String em){

      super(fn, ln, addr, em);

      password = pw;

      customerID = cid;

      list = new AccountList2();

   }

//Behaviors

   public void setPw(int pw){

      password = pw;

   }

   public int getPw(){

      return password;

   }

   public void setList(AccountList2 li){

      list = li;

   }

   public AccountList2 getList(){

      return list;

   }

   public void setCid(int cid){

      customerID = cid;

   }

   public int getCid(){

      return customerID;

   }

   public void addAccount(Account2 acc){

      list.addAccount2(acc);

   }

   public void display(){

      System.out.println("");

      System.out.println("Password: " + getPw());

      System.out.println("Customer ID: " + getCid());

      super.display();

      list.display();

      System.out.println("");

   }

   public void select(String custId) throws URISyntaxException{

      try{

         customerID = 0;

         password = 0;

         setFn(null);

         setLn(null);

         setEm(null);

         setAddr(null);

         Account2 acc=new Account2();

         String line;

     

         URL path = ClassLoader.getSystemResource("Customer.txt");

         System.out.println("path is "+path);

        File f = new File(path.toURI());

     

      

      // Scanner input = new Scanner(f);

      // File f1 = new File("Customer.txt");

         Scanner inFile = new Scanner(f);

         while(inFile.hasNextLine()){

            line = inFile.nextLine();

            StringTokenizer tokenizer = new StringTokenizer(line, ":");

            String token = tokenizer.nextToken();

            System.out.println(token);

            if(token.equals(custId)){

               customerID = Integer.parseInt(token);

               password = Integer.parseInt(tokenizer.nextToken());

               setFn(tokenizer.nextToken());

               setLn(tokenizer.nextToken());

               setAddr(tokenizer.nextToken());

               setEm(tokenizer.nextToken());

               list = new AccountList2();

           

               URL path1 = ClassLoader.getSystemResource("Accounts.txt");

               System.out.println("path1 is "+path);

               File f1 = new File(path1.toURI());

           

            

            // Scanner input = new Scanner(f);

               Scanner accFile = new Scanner(f1);

               while(accFile.hasNextLine())

               {

                  tokenizer = new StringTokenizer(accFile.nextLine(), ":");

                  String accNo = tokenizer.nextToken(); // acount number

                  String cid = tokenizer.nextToken();

                  if(cid.equals(custId))

                  {

                     acc.setAccNo(Integer.parseInt(accNo));

                     acc.setCid(Integer.parseInt(cid));

                     acc.setAccType(tokenizer.nextToken());

                     acc.setBal(Double.parseDouble(tokenizer.nextToken()));

                  //list.add(Integer.parseInt(accNo));

                  }

               }

               accFile.close();

               break;

            }

         }

         inFile.close();

      }

      catch(IOException ie){

         System.out.println(ie);

      }  

   }

   public static void main(String[] args) throws URISyntaxException{

      Customer2 c2;

      c2 = new Customer2();

      c2.display();

  

      c2.select("3003");

      c2.display();

   }

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.*;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.URISyntaxException;

import java.net.URL;

import java.util.Scanner;

import java.util.StringTokenizer;

public class Account2{

//Properties

   private int accountNo;

   private int customerId;

   private String accountType;

   private double balance;

//Constructors

   public Account2(){

      accountNo = 0;

      customerId = 0;

      accountType = "";

      balance = 0.0;

   }

   public Account2(int acc, int cid, String accType, double bal){

      accountNo = acc;

      customerId = cid;

      accountType = accType;

      balance = bal;

   }

//Behaviors

   public void setAccNo(int acc){

      accountNo = acc;

   }

   public int getAccNo(){

      return accountNo;

   }

   public void setCid(int cid){

      customerId = cid;

   }

   public int getCid(){

      return customerId;

   }

   public void setAccType(String accType){

      accountType = accType;

   }

   public String getAccType(){

      return accountType;

   }

   public void setBal(double bal){

      balance = bal;

   }

   public double getBal(){

      return balance;

   }

   public void display(){

      System.out.println("===================== ");

      System.out.println("Account Number: " + getAccNo());

      System.out.println("Customer ID: " + getCid());

      System.out.println("Account Type: " + getAccType());

      System.out.println("Balance: $" + getBal());

      System.out.println("");

   }

   public void select(String accNo) throws FileNotFoundException, URISyntaxException{  

      accountNo = 0;

      accountType = "";

      balance = 0;

      customerId = 0;

      try{

         String line;

     

         URL path = ClassLoader.getSystemResource("Account.txt");

         System.out.println("path is "+path.toString());

         File f = new File(path.toURI());

         Scanner inFile = new Scanner(f);

         while(inFile.hasNextLine()){

           line = inFile.nextLine();

            StringTokenizer tokenizer = new StringTokenizer(line, ":");

            String token = tokenizer.nextToken();

            System.out.println(token);

            if(token.equals(accNo)){

               accountNo = Integer.parseInt(token);

               customerId = Integer.parseInt(tokenizer.nextToken());

               accountType = tokenizer.nextToken();

               balance = Double.parseDouble(tokenizer.nextToken());

               break;

            }

         }

         inFile.close();

      }

      catch(IOException ie){

         System.out.println(ie);

      }

   }

   public static void main(String[] args) throws FileNotFoundException, URISyntaxException{

      Account2 a1 = new Account2();

      a1.select("9000");

      a1.display();

   }

}

---------------------------------------------------------------------------------------------

public class Person2{
//Properties
private String firstName;
private String lastName;
private String address;
private String email;
//Constructors
public Person2(){
firstName = "";
lastName = "";
address = "";
email = "";
}
public Person2(String fn, String ln, String addr, String em ){
firstName = fn;
lastName = ln;
address = addr;
email = em;
}
//Behaviors
public void setFn(String fn){
firstName = fn;
}
public String getFn(){
return firstName;
}
public void setLn(String ln){
lastName = ln;
}
public String getLn(){
return lastName;
}
public void setAddr(String addr){
address = addr;
}
public String getAddr(){
return address;
}
public void setEm(String em){
email = em;
}
public String getEm(){
return email;
}
public void display(){
System.out.println("");
System.out.println("First Name: " + getFn());
System.out.println("Last Name: " + getLn());
System.out.println("Address: " + getAddr());
System.out.println("E-Mail: " + getEm());
System.out.println("");
}

public static void main(String[] args){
Person2 p2;
p2 = new Person2("Billy","Carter","Georgia","bc@yahoo.com");
p2.display();
Person2 p3;
p3 = new Person2("Tony","Danza","Atlanta","gggg@hotmail.com");
p3.display();
}
}

Explanation / Answer

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class YourClass {

   String fileName;

   public class Customer{

      String uid;

      String password;

      String firstName;

      String lastName;

      String email;

   }

   private Customer selected;

   public boolean isValid(String userId, String password) {

      selected = null;

      try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))){          

          String line;

          while ((line = br.readLine())!=null) {

              String[] tokens = line.split(":");

              for (int i = 0; i< tokens.length; i++)

                  tokens[i] = tokens[i].substring(1, tokens[i].length()-1);

              if (tokens[0].equals(userId) && tokens[1].equals(password)) {

                  selected = new Customer();

                  selected.uid = tokens[0];

                  selected.password = tokens[1];

                  selected.firstName = tokens[2];

                  selected.lastName = tokens[3];

                  selected.email = tokens[4];

                  return true;

              }

             

          }

         

      } catch (FileNotFoundException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

      } catch (IOException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

      }

      return false;

   }

}


Here is a demo of how you should proceed. You can modify it to meet your requirements. Presently it returns true and populates the customer object if match is found.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote