2016-11-15 2 views
1

この質問はすでに以前に尋ねられていますが、これを見つけようとしていて、私の問題と一致するものは何も見つかりませんでした。私の問題は、javaのgui(私はnetbeansを使用する)の.txtファイルに基づいてログインデータを検証する方法です。 私は2つのjFrameを持っています、最初はMainMenuフレームと登録フレームです、そして、私はMainMenuで立ち往生しました。登録クラスでは、データを.txtファイルに保存しても問題ありません。ここで.txtファイルの サンプル:java guiのテキストファイルに基づいてログインデータを検証する方法

名前:Gifhary年齢:20金額:2000口座番号:1234パスワード:blabla

ファイル名が無いアカウントに基づいています。

これは私の登録コード

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: 
} 

である私は

「コードの生成」にいくつかのデフォルトのコードをスキップして、これがメインメニュー

ための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: 
}  

データであり、パスワード 誰かが私を助けることができるなら、お願いします。あなたに非常に感謝しています。

答えて

0

情報をファイルに保存できるので、それを読み返す方法を知っておく必要があります。ここでは、ファイルからパスワード文字列を取得するyourReaderという関数でこの機能を実装したと仮定します。そこから、実装の残りの部分は簡単です:

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!"); 
} 

は編集:私はyourReaderのためにあなたにヒントを与えるでしょう:

String allLines = ..?; 
String searchString = "Password: "; 
String password = allLines.substring(allLines.indexOf(searchString) + searchString.length()); 
return password; 
+0

私はにGUIを構築する際に、user1092688 @ありがとうけど。 jar これ以上データを.txtファイルに保存しません winRarで.jarファイルを探して.txtファイルを保存する宛先フォルダを変更しました.jarファイルはそこに2つのフォルダしかありませんでした。バンクシミュレータとMETA-INF 最初のフォルダの宛先は(まだnetbeansで開いています) "BankSimulator/sr c/banksimulator/Registration/" .jarファイルは" banksimulator/Registration/" –

+0

です。これは、' src/banksimulator/... 'で相対パス**を指定したためです。** absolute絶対パスに保存する場合は、パスC:\\ Users \\ Gifhary \\ Desktop'を選択します。 .jarの現在の場所に保存する場合は、[this technique](http://stackoverflow.com/a/7603444/1092688)を使用してください。 –

関連する問題