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

时间:2019-04-01 11:44:54

标签: java jvm

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

这是我的数据库。

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();
    }
}

1 个答案:

答案 0 :(得分: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处,帐户名将为。

祝你好运。