从文本文件中调用确切的值

问题描述 投票:0回答:1

嗨,大家需要帮助我的学校迷你项目。如何比较用户输入和匹配文本文件中的数据库。这就像用户名和密码的有效性。我想使用帐号和密码在我的数据库上调用第二行。

这是我的数据库。

0,admin,adminLastName,123456,123456
1,user,userLastName,1234567,123456

0 = id
admin = name
adminLastName = Last Name
1234567 = accountNumber
123456 = pin

这是我的代码。

package atm;

import java.io.File;
import java.util.Scanner;

public class Login {

 static void verifyLogin(String name, String lastName, String userAccountNumber, String userPin, String filePath){
    Scanner inputData = new Scanner(System.in);
    boolean isFound = false;
    String tempAccountNumber = "";
    String tempPin = "";

     System.out.print("\nAccount Number: ");
     userAccountNumber = inputData.next();

     System.out.print("\nPIN: ");
     userPin = inputData.next();

        try{
            Scanner readTextFile = new Scanner(new File("myDataBase.txt")).useDelimiter("[,\n]");
            while (readTextFile.hasNext() && !isFound){
                tempAccountNumber = readTextFile.next();
                tempPin = readTextFile.next();
                if (tempAccountNumber.trim().equals(userAccountNumber.trim()) && tempPin.trim().equals(userPin.trim())){
                    isFound = true;
                    System.out.println("Welcome " + name+ " " +lastName);
                    System.out.println("\nLogin Successfully!");
                }
                else {
                    System.out.println("You have entered your PIN or ACCOUNT NUMBER incorrectly. Please check your PIN or ACCOUNT NUMBER and try again.\n If you don't have account yet please go to SignUp page!\n");
                    myMain mainMenu = new myMain();
                    mainMenu.inputKeyboard();
                }
            }
            readTextFile.close();
        }
        catch (Exception e){
        }
    inputData.close();
    }
}
java jvm
1个回答
0
投票

如果你的文本文件每行包含1个用户,然后用','分割它,那么你可以像你一样取每一行,然后将该行拆分成一个string []数组,并检查是否名称对应'admin'。

public class Main {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

    Boolean loggedin = false;

    String fileName = "accounts.txt";
    String line = null;

    System.out.println("What's your username?");
    String tempUsername = input.nextLine();

    System.out.println("What's your password?");
    String tempPassword = input.nextLine();

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            String[] currAccount = line.split(",");

            if (currAccount[1].equals(tempUsername) && currAccount[4].equals(tempPassword)) {
                loggedin = true;

                System.out.println("You have successfully logged in!");
            }
        }

        bufferedReader.close();
    }
    catch(FileNotFoundException ex) {
        ex.printStackTrace();

        // Let's create it if file can't be found or doesn't exist, but let's ask first.

        String answer;

        System.out.print("File not found, do you want to create it? [Y/n]: ");
        answer = input.nextLine();

        if (answer.equalsIgnoreCase("y")) {
            try {
                FileWriter fileWriter = new FileWriter(fileName);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

                System.out.println("File has been created!");
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        } else {
            System.out.println("File was not created!");
        }

    }
    catch(IOException ex) {
        ex.printStackTrace();
    }

    if (!loggedin) {
        System.out.println("Your login combination did not exist.");
    }
}
}

请注意,我没有评论过很多,但它仍然有意义。

拆分后记住你从数组索引0开始,而不是1.因此在索引1处,帐户上的名称将是。

祝好运。

© www.soinside.com 2019 - 2024. All rights reserved.