1

i'm sorry if this question is already asked before, but i have tried to find this and i got nothing which match with my problem. my problem is how to validating login data based on .txt file in java gui (i use netbeans). i have 2 jFrame, first is MainMenu frame and Registration frame and i stuck in MainMenu. In registration class is no problem with saving data into .txt file. sample of .txt file here :

Name: Gifhary Age: 20 Amount: 2000 Account No: 1234 Password: blabla

file name is based on account no.

this is my Registration code

package banksimulator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Registration extends javax.swing.JFrame {
static ArrayList name = new ArrayList();
static ArrayList age = new ArrayList();
static ArrayList amount = new ArrayList();
static ArrayList accountNo = new ArrayList();
static ArrayList password = new ArrayList();

/**
 * Creates new form Registration
 */
public Registration() {
    initComponents();
    this.setLocationRelativeTo(null);
private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    String content = "Name: "+txtName.getText()+"  Age: "+txtAge.getText()+"  Amount: "+txtAmount.getText()+"  Account No: "+txtAccountNo.getText()+"  Password: "+txtPassword.getText();
    try {

        File file = new File("src/banksimulator/Registration/"+txtAccountNo.getText()+".txt");

        if (!file.exists()) {
             file.createNewFile(); 


        FileWriter data = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(data);
        bw.write(content);
        bw.close();

        JOptionPane.showMessageDialog(this, "Data Is Saved");

        txtName.setText("");
        txtAge.setText("");
        txtAmount.setText("");
        txtAccountNo.setText("");
        txtPassword.setText("");
        }
        else {
          JOptionPane.showMessageDialog(this, "Account Number Is Already Used");
          txtAccountNo.setText("");
        }
    } catch (IOException e) {
    }

   // TODO add your handling code here:
}                                       

private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    this.dispose();
    MainMenu m = new MainMenu();
    m.setVisible(true);
    // TODO add your handling code here:
}

i skip some default code in "Generate Code"

and this is for MainMenu

 package banksimulator;

 import java.io.*;
 import javax.swing.*;

 /**
 *
 * @author Qodri
 */
 public class MainMenu extends javax.swing.JFrame {



/**
 * Creates new form MainMenu
 */
public MainMenu() {
    initComponents();

    this.setLocationRelativeTo(null);
}
private void registrationBtnActionPerformed(java.awt.event.ActionEvent evt) {                                                
    this.dispose();
    Registration r = new Registration();
    r.setVisible(true);

    // TODO add your handling code here:
}                                               

private void logInBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String account = txtAccountNo.getText();
    String password = txtPassword.getText();


//My problem is here


    // TODO add your handling code here:
}     

data required for MainMenu is Account No and Password if anyone can help me, please. and very thanks to you

1 Answers1

0

Since you can save the information to a file, you should know how to read it back. For now I'll assume you've implemented this capability in a function called yourReader which gets the password string from a file. From there, the rest of the implementation is straightforward:

String account = txtAccountNo.getText();
String password = txtPassword.getText();
String correctPassword = yourReader("src/banksimulator/Registration/" + account + ".txt);//???
if(password.equals(correctPassword)){
    System.out.println("Login successful.");
}
else {
    System.out.println("Hacker alert!");
} 

Edit: I'll give you a hint for yourReader:

String allLines = ..?;
String searchString = "Password: ";
String password = allLines.substring(allLines.indexOf(searchString) + searchString.length());
return password;
Eric M.
  • 642
  • 5
  • 16
  • thank you @user1092688 but, when i build the GUI into .jar it doesn't save data to .txt file anymore i have change the destination folder to save .txt file by looking .jar file with winRar, in .jar file is only 2 folder there, banksimulator and META-INF at first folder destination is (still open by netbeans) "BankSimulator/src/banksimulator/Registration/" in .jar file is "banksimulator/Registration/" – Gifhary Syidhqa Hamim Nov 16 '16 at 17:01
  • That's because you specified a **relative path** with `src/banksimulator/...`, you can specify an **absolute path** i.e. `C:\\Users\\Gifhary\\Desktop` if you want to save to an absolute location. If you want to save to the current location of your .jar, you can use [this technique](http://stackoverflow.com/a/7603444/1092688) – Eric M. Nov 16 '16 at 17:51