将文本文件中的数据与 Java 中的用户输入进行匹配

发布于 2024-09-19 07:54:21 字数 1432 浏览 4 评论 0原文

我需要从记事本文件(如下)中检索 PIN 码,并使用用户输入的 PIN 码进行检查。我已经尝试了好几天了,但到目前为止,我提出的解决方案仅在我输入整行(即 1598 01-10-102203-0 95000)时才给我正确的输出。它还为每条记录显示“无效 PIN”。

PIN AccountNo   Balance
1598    01-10-102203-0  95000
4895    01-10-102248-0  45000
9512    01-10-102215-0  125000
6125    01-10-102248    85000

Output - You have login!

    Invalid PIN    
    Invalid PIN
    Invalid PIN
BufferedReader getIt = new BufferedReader(new InputStreamReader(System.in));
String userPIN = "";

try {
    // Open the file that is the first command line parameter
    FileInputStream fstream = new FileInputStream(
        "D:\\Studies\\BCAS\\HND\\Semester 1\\Programming "
        + "Concepts\\Assignment\\AccountInfo.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    System.out.println("Enter PIN");
    userPIN = getIt.readLine();

    while ((strLine = br.readLine()) != null) {
        // Print the content on the console#    
        if (userPIN.equals(strLine)) {
            System.out.println("You have login!");
        } else {
            System.out.println("Invalid PIN!");
        }
    }
    //Close the input stream
    in.close();
} catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

I need to retrieve the PIN from a notepad file (below) and check it with the PIN which the user has typed. I have tried this for days, but so far the solution that I have come up with gives me the correct output only when I type the full row (i.e. 1598 01-10-102203-0 95000). Also it displays the "Invalid PIN" for each and every record.

PIN AccountNo   Balance
1598    01-10-102203-0  95000
4895    01-10-102248-0  45000
9512    01-10-102215-0  125000
6125    01-10-102248    85000

Output - You have login!

    Invalid PIN    
    Invalid PIN
    Invalid PIN
BufferedReader getIt = new BufferedReader(new InputStreamReader(System.in));
String userPIN = "";

try {
    // Open the file that is the first command line parameter
    FileInputStream fstream = new FileInputStream(
        "D:\\Studies\\BCAS\\HND\\Semester 1\\Programming "
        + "Concepts\\Assignment\\AccountInfo.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    System.out.println("Enter PIN");
    userPIN = getIt.readLine();

    while ((strLine = br.readLine()) != null) {
        // Print the content on the console#    
        if (userPIN.equals(strLine)) {
            System.out.println("You have login!");
        } else {
            System.out.println("Invalid PIN!");
        }
    }
    //Close the input stream
    in.close();
} catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-09-26 07:54:21

Jon Freedman 出色的答案,您应该考虑接受,是您必须将传入的文本行分解为其组成部分,以便将它们与键入的内容进行比较。这是一种方法:

String line = "1598 01-10-102203-0 95000";
for (String s : line.split(" ")) {
    System.out.println(s);
}

这会产生以下输出:

1598
01-10-102203-0
95000

附录:

while ((strLine = br.readLine()) != null) {
    String[] a = strLine.split(" ");
    // now the array a contains the three parts
    ...
}

An essential element of Jon Freedman's excellent answer, which you should consider accepting, is that you must break up the incoming line of text into its component parts in order to compare them to what is typed. Here's one approach:

String line = "1598 01-10-102203-0 95000";
for (String s : line.split(" ")) {
    System.out.println(s);
}

This produces the following output:

1598
01-10-102203-0
95000

Addendum:

while ((strLine = br.readLine()) != null) {
    String[] a = strLine.split(" ");
    // now the array a contains the three parts
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文