在Java中解析整个文本文件

在Java中解析整个文本文件,java,Java,我正在尝试读取一个文本文件,查找最终项目的用户名和密码。我可以让扫描仪读一行,但只能读一行。我还需要能够传递用户名信息,以便根据凭证文件中包含的角色打印文件。目前,它只会逐行验证。如果我从凭证文件的第一行正确输入用户名和密码,它将按预期工作。如果输入错误,它将只接受凭据文件第二行中的用户名和密码 我的问题是如何正确解析凭证文件以搜索整个文件,而不仅仅是一行 我不需要担心散列,只需要括号中的密码。然后我还必须打印另一个文本文件,该文件引用每行中的第四项,但我还没有做到这一点。。任何帮助都将不胜感激

我正在尝试读取一个文本文件,查找最终项目的用户名和密码。我可以让扫描仪读一行,但只能读一行。我还需要能够传递用户名信息,以便根据凭证文件中包含的角色打印文件。目前,它只会逐行验证。如果我从凭证文件的第一行正确输入用户名和密码,它将按预期工作。如果输入错误,它将只接受凭据文件第二行中的用户名和密码

我的问题是如何正确解析凭证文件以搜索整个文件,而不仅仅是一行

我不需要担心散列,只需要括号中的密码。然后我还必须打印另一个文本文件,该文件引用每行中的第四项,但我还没有做到这一点。。任何帮助都将不胜感激

文本文件:

griffin.keyes   108de81c31bf9c622f76876b74e9285f    "alphabet soup" zookeeper
rosario.dawson  3e34baa4ee2ff767af8c120a496742b5    "animal doctor" admin
bernie.gorilla  a584efafa8f9ea7fe5cf18442f32b07b    "secret password"    veterinarian
donald.monkey   17b1b7d8a706696ed220bc414f729ad3    "M0nk3y business"   zookeeper
jerome.grizzlybear  3adea92111e6307f8f2aae4721e77900    "grizzly1234"   veterinarian
bruce.grizzlybear   0d107d09f5bbe40cade3de5c71e9e9b7    "letmein"   admin
我的代码:

public static void main(String[] args)throws FileNotFoundException  {
File file = new File ("C:\\Users\\Rick\\Documents\\NetBeansProjects\\IT145finalproject4\\src\\it145finalproject4\\credentials.txt");

    String passWord;
    String userName;
    Scanner scnr = new Scanner (file);
    Scanner sc = new Scanner (System.in);



    while(scnr.hasNextLine()){
        int attempts = 0;
        for (int i = 0; i < 3; i++) {

        String line = scnr.nextLine();
        System.out.println("Enter a username:" );
        userName = sc.nextLine();            
        System.out.println("Enter a password:");  
        passWord = sc.nextLine();


            if(line.contains(userName) && (line.contains (passWord))){
              return;                
            }

            if (!line.contains(userName) && (!line.contains (passWord))){
                System.out.println("Please try again.");
            }

            attempts++;
            if (attempts == 3){
                System.out.println("Maximum attempts reached program exiting.");
            } 
        }
    }
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
File File=new文件(“C:\\Users\\Rick\\Documents\\NetBeansProjects\\IT145finalproject4\\src\\IT145finalproject4\\credentials.txt”);
字符串密码;
字符串用户名;
扫描仪scnr=新扫描仪(文件);
扫描仪sc=新的扫描仪(System.in);
while(scnr.hasNextLine()){
int=0;
对于(int i=0;i<3;i++){
String line=scnr.nextLine();
System.out.println(“输入用户名:”);
用户名=sc.nextLine();
System.out.println(“输入密码:”);
密码=sc.nextLine();
if(line.contains(用户名)和&(line.contains(密码))){
回来
}
如果(!line.contains(用户名)&&(!line.contains(密码))){
System.out.println(“请再试一次”);
}
尝试++;
如果(尝试次数==3){
System.out.println(“程序退出时达到的最大尝试次数”);
} 
}
}
}

我相信这项修正案解决了您所描述的问题:

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

public class TestClass
{
public static void main(String[] args) throws FileNotFoundException  
{
    File file = new File ("C:\\Users\\Mel\\Documents\\test.txt");

    String passWord = "";
    String userName = "";
    Scanner scnr = new Scanner (file);
    Scanner sc = new Scanner (System.in);
    int attempts = 0, numLines = 0, count = 0;

    //Get the number of lines in the file
    while(scnr.hasNext())
    {
        numLines++;
        scnr.nextLine();
    }

    //Reset the scanner
    scnr.close();
    scnr = new Scanner(file);

    while(scnr.hasNextLine())
    {
        count++;
        String line = scnr.nextLine();

        //only run this code once per file iteration (at the start)
        if (count == 1)
        {
            System.out.println("Enter a username:" );
            userName = sc.nextLine();            
            System.out.println("Enter a password:");  
            passWord = sc.nextLine();
        }

        if(line.contains(userName) && (line.contains (passWord))){
            return; //success!               
        }

        //only execute this code once the entire file has been scanned!
        if (count == numLines)
        {
            attempts++;
            if (attempts == 3){
                System.out.println("Maximum attempts reached program exiting.");
                return;
            } 
            if (!line.contains(userName) || (!line.contains (passWord))){
                System.out.println("Please try again.");
                //reset scanner!
                scnr.close();
                scnr = new Scanner(file);
                count = 0;
            }
        }
    }
}
}
我不确定这是否是最好的解决方案,但我认为这是一个进步。您的问题主要在于没有重置扫描仪,而且调用输入方法过于频繁。您将看到,最初计算的行数有助于确定何时应该调用某些代码


我还删除了for循环,因为它似乎与代码没有任何关系。

看一看。具体描述你的问题,包括你尝试过的代码,而不是场外截图,等等。我想当你检查这行是否有
用户名
密码
时,你会退出while循环。另外,你能发布你的代码吗(通过复制/粘贴而不是图像)对不起,我急于发布它。我整天都在做这件事,结果一事无成。你有什么具体问题吗?