有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

正则表达式Java故障查找字符串

我很难理解为什么这不起作用。这只是代码中不起作用的部分

用户名和密码作为参数传递给方法。我已经运行了testprint语句,以确保传递的值是正确的,并且正确分配了文件的下一行

fileByteStream = new FileInputStream("credentials.txt");  
inFS = new Scanner(fileByteStream);        


while (inFS.hasNextLine()){                          // If the file has another line
String nextLine = inFS.nextLine();                   // Assign that line to a String

if (nextLine.contains(username)) {                   // Check line for username             
    System.out.println("USERNAME FOUND!");                    

        if (nextLine.contains(password)) {           // Verify password
            System.out.println("PASSWORD FOUND!");

这是我的困境。下一行。contains(用户名)未按预期工作。它匹配行中任意位置的实例

我尝试过使用(“\\b”+用户名+“\\b”),但是,它似乎没有在行中找到用户名,我无法找出原因

如果用户名匹配,则查找密码似乎工作正常

文本文件中的行的格式如下:

用户。名称MD5hashOfPassword密码作业


共 (3) 个答案

  1. # 1 楼答案

    谢谢大家的帮助。这就是最终为我工作的原因

    fileByteStream = new FileInputStream("credentials.txt");  
    inFS = new Scanner(fileByteStream);        
    
    
    while (inFS.hasNextLine()){                                 // If the file has another line
        String nextLine = inFS.nextLine();                      // Assign that line to a String
        String[] lineOfCredentials = nextLine.split("\\s+") 
    
            if (lineOfCredential[0].equals(username)               // Check first item of array             
                System.out.println("USERNAME FOUND!");                    
    
                    if (lineOfCredentials[1].equals(password)) {             // Verify password
                        System.out.println("PASSWORD FOUND!");
                        fileByteStream.close();
                        return (lineOfCredentials[lineOfCredentials.length - 1);
    }
    

    再次感谢你的帮助。非常感谢您的反馈

  2. # 2 楼答案

    您需要对每一行进行某种预处理。提取要匹配的部分,以便进行精确检查

    请注意,我在本例中使用了^{},因为它与您提供的示例数据一起工作,但我不知道该行的实际语法

    String nextLine = inFS.nextLine();
    String[] parts = nextLine.split("\\w+"); // '+' allows multiple spaces
    String usernameFromLine = parts[0];
    String passwordFromLine = parts[2];
    
    if(usernameFromLine.equals(username)) {
        ....
    

    请注意,这个示例既不检查边界,也不关心语法错误的行。这两种情况都会崩溃

  3. # 3 楼答案

    您已经在使用Scanner,它可以进行字符串标记化,但并没有真正使用它的功能。您可以使用^{}读取标记,而不是读取整行代码,然后自己完成工作。如果

        while (inFS.hasNextLine()) {                      
            /* Get the username */
            String nextUsername = inFS.next();
    
            // Is this the username you're looking for?
            if (nextUsername.contains(username)) {
                System.out.println("USERNAME FOUND!");
    
                /* Consume the next token, even if you're not going to
                    do anything with it */
                String nextMD5 = inFS.next();
    
                // Next token should be the password
                String nextPassword = inFS.next();
                if (nextPassword.contains(password)) {
                    System.out.println("PASSWORD FOUND!");
                }
            }
    
            // ensure you advance to the next line before starting again
            inFS.nextLine();
        }