Java:如何将txt文件中的多个句子拆分为数组,然后在数组中搜索单个单词?

标签 java arrays

我被困在这里的一个项目中,我们得到了一个 txt 文件,其中包含多个句子,其中包含用户名、哈希密码和用户角色。我设法使用 split 函数将句子拆分为单独的数组,但现在我不知道如何在每个单独的句子中搜索匹配的用户名/密码。非常感谢任何帮助。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.util.Scanner;

public class Execute {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Scanner scnr = new Scanner(System.in);
        FileInputStream fileByteStream = null;
        String username = "";
        String password = "";

        Authentication user = new Authentication();

        // Ask for user input
        System.out.println("Enter your username");
        username = scnr.nextLine();

        System.out.println("Enter your password");
        password = scnr.nextLine();

        // Convert the password string to an MD5 hash
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        // Set the username and password to what the user entered
        user.setUsername(username);
        user.setPassword(sb.toString());

        // open the credentials file
        try {
            fileByteStream = new FileInputStream("src/credentials.txt");
        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }
        Scanner inFS = new Scanner(fileByteStream);
        // inFS.useDelimiter("");

        int lineNumber = 1;
        while (inFS.hasNextLine()) {
            String fileLine = inFS.nextLine(); // declares string "fileLine" to
                                            // the next line in the file
            String[] fileLineArray = fileLine.split("; "); // turns fileLine into
                                                        // an array name
                                                        // fileLineArray
            lineNumber++; // increase line number by 1 each progression

            if (fileLineArray[0].matches(user.getUsername())) {
                System.out.println("Success");
            } else {
                System.out.println("Failed");
            }
        }
    }
}

我不知道这是否有帮助,但这是另一门课。

public class Authentication {
    private String username;
    private String password;

    public Authentication(){
        setUsername("");
        setPassword("");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这也是凭证文件

griffin.keyes 108de81c31bf9c622f76876b74e9285f zookeeper;

rosario.dawson 3e34baa4ee2ff767af8c120a496742b5 admin;

bernie.gorilla a584efafa8f9ea7fe5cf18442f32b07b veterinarian;

donald.monkey 17b1b7d8a706696ed220bc414f729ad3 zookeeper;

jerome.grizzlybear 3adea92111e6307f8f2aae4721e77900 veterinarian;

bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7 admin;

最佳答案

    if (fileLineArray[0].contains(user.getUsername())) {
        System.out.println("Success");

    } else {
        System.out.println("Failed");
    }

尝试使用 contains 因为

fileLineArray[0] is griffin.keyes 108de81c31bf9c622f76876b74e9285f zookeeper

或者尝试进行不同的分割并在文件中按行搜索

使用

readLine()

并在文件中使用 token 进行更好的分割

griffin.keyes-108de81c31bf9c622f76876b74e9285f-zookeeper


String[] fileLineArray = fileLine.split("-");
fileLineArray[0] will be griffin.keyes

关于Java:如何将txt文件中的多个句子拆分为数组,然后在数组中搜索单个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41064491/

相关文章:

java - HTTPS nonProxyHosts 的 JVM 参数

Java - 有多少字符串连接应该提示使用 StringBuilder?

java - 在这种情况下,CommandLineRunner 做了什么?

java - 将数组中的元素复制到不同类型

比较c中的两个整数数组很奇怪

php - 检查数组关键参数并使用适当数量的参数调用方法

java - 将 perf4j 性能统计数据公开给 JMX 不起作用

java - ArrayList<SensorEvent>.add(...) 开始覆盖值

c++ - C4838 警告与 const char* 数组的数组初始化

c++ - 非静态数据成员 c++ 的无效使用