简体   繁体   English

Java扫描程序类从文本文件读取制表符分隔的字符串

[英]Java scanner class reading tab delimited strings from text file

I am a student learning Java coding. 我是一名学习Java编码的学生。 I am trying to check a username and password within a text file. 我正在尝试检查文本文件中的用户名和密码。 If the text file contains the username, then the 3 next fields are assigned to variables. 如果文本文件包含用户名,则接下来的3个字段将分配给变量。

The text file has 4 tab-delimited fields and 5 unique lines. 文本文件具有4个制表符分隔的字段和5个唯一的行。

If I enter the username and password from the first line of a text file, then I can get boolean values to return true. 如果从文本文件的第一行输入用户名和密码,则可以获取布尔值以返回true。 If I enter any other username or password the boolean values remain false. 如果我输入任何其他用户名或密码,则布尔值保持为false。 I use a print method and can tell that the while loop iterates thru the entire file. 我使用一种打印方法,并告诉while循环遍历整个文件。

I'm trying to figure out why the other lines of the text files aren't being compared to the username or password variables. 我试图弄清楚为什么没有将文本文件的其他行与用户名或密码变量进行比较。

My code: 我的代码:

while (inFS.hasNext() && !userGood && !passGood) {
    credString = inFS.next();
    System.out.println(credString);

    if (userName.equals(credString)) {
        userGood = true;
        System.out.println("*****userGood TRUE");
        encryptedPass = inFS.next();
        unencryptedPass = inFS.next();
        userRole = inFS.next();
        if (sb.toString().equals(encryptedPass)){
            passGood = true;
            System.out.println("*****passGood TRUE");
            break;
      }                                                           
 }                       

} }

问题在于next()方法不会跳过一行,因此您应该在进行下一个while迭代之前进行nextLine()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM