Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用while()循环检查.txt文件中的元素时出现问题_Java_Arrays_File_While Loop - Fatal编程技术网

Java 使用while()循环检查.txt文件中的元素时出现问题

Java 使用while()循环检查.txt文件中的元素时出现问题,java,arrays,file,while-loop,Java,Arrays,File,While Loop,我正在编写一个程序,要求用户登录并检查他们的ID是否在“Employees.txt”文件中,如果是,那么如果他们是经理或助理,它将打印出有效的ID。但是当我输入一个无效的ID时,说“x”,然后再试一次,它将永远循环,不检查ID是否有效。如何解决此问题?我的Employees.txt与Account.java和 E12,Manager E13,Assistant 是文件里面的东西。任何帮助都将不胜感激,因为我已经尽力了。谢谢 import java.io.*; import java.util.

我正在编写一个程序,要求用户登录并检查他们的ID是否在“Employees.txt”文件中,如果是,那么如果他们是经理或助理,它将打印出有效的ID。但是当我输入一个无效的ID时,说“x”,然后再试一次,它将永远循环,不检查ID是否有效。如何解决此问题?我的Employees.txt与Account.java和

E12,Manager
E13,Assistant
是文件里面的东西。任何帮助都将不胜感激,因为我已经尽力了。谢谢

import java.io.*;
import java.util.*;
public class Account
{
    public static String  accountInput;
    public static boolean inSystem      = true;
    public static boolean displayLogIn  = true;
    public static boolean managerLogIn  = false;
    public static Scanner userInput     = new Scanner(System.in);
    public static File stockFile        = new File("Stock.txt");
    public static File employeesFile    = new File("Employees.txt");
    public static File transactionsFile = new File("Transactions.txt");
    public static void main(String[] args) throws IOException
    {
        String[] contents;
        System.out.println("\tWorking Files");
        System.out.println("-------------------------------------------");
        System.out.println("Stock File:\t\tStock.txt\nEmployee File:\t\tEmployees.txt\nTransactions File:\tTransactions.txt\n\n");
        Scanner readEmployeesFile = new Scanner(employeesFile);
        while(inSystem)
        {
            while(displayLogIn)
            {
                System.out.print("Enter Employee ID(i.e E1)\tEnter Q to Quit Program.\nEnter here: ");
                accountInput = userInput.nextLine();

                while(readEmployeesFile.hasNextLine())
                {
                    contents = readEmployeesFile.nextLine().split(",");
                    if(contents[0].equals(accountInput) & contents[1].equals("Manager"))
                    {
                        displayLogIn = false;
                        System.out.print("Valid MGR ID");
                        inSystem     = false;
                    }
                    else if(contents[0].equals(accountInput) & contents[1].equals("Assistant"))
                    {
                        displayLogIn = false;
                        System.out.print("Valid AST ID");
                        inSystem     = false;
                    }
                    else if(accountInput.equals("Q"))
                    {
                        displayLogIn = false;
                        inSystem     = false;
                    }
                }
            }
        }
    }
}
改变

contents[0].equals(accountInput) & contents[1].equals("Manager")
contents[0].equals(accountInput) & contents[1].equals("Assistant")


运算符用双和(&&&)表示,而不是一个。

问题是您在循环中打开了
扫描仪
,迫使它在第一次迭代时读取文件的所有内容。这意味着
while(readEmployeesFile.hasNextLine())
中的条件在第一次迭代后将始终计算为
false
,并且循环将永远不会再次进入。因此,只有第一次试用可能有效,之后循环无法终止

以下是适用于我的调整代码:

import java.io.*;
import java.util.*;
public class Account
{
    public static String  accountInput;
    public static boolean inSystem      = true;
    public static boolean displayLogIn  = true;
    public static Scanner userInput     = new Scanner(System.in);
    public static File employeesFile    = new File("Employees.txt");
    public static void main(String[] args) throws IOException
    {
        List<String[]> contents = new ArrayList<>();
        System.out.println("\tWorking Files");
        System.out.println("-------------------------------------------");
        System.out.println("Stock File:\t\tStock.txt\nEmployee File:\t\tEmployees.txt\nTransactions File:\tTransactions.txt\n\n");
        Scanner readEmployeesFile = new Scanner(employeesFile);
        while(readEmployeesFile.hasNextLine()) {
            String[] current = readEmployeesFile.nextLine().split(",");
            contents.add(current);
        }
        while(inSystem)
        {
            while(displayLogIn)
            {
                System.out.print("Enter Employee ID(i.e E1)\tEnter Q to Quit Program.\nEnter here: ");
                accountInput = userInput.nextLine();

                for(String[] content: contents) {
                    if (content[0].equals(accountInput) & content[1].equals("Manager")) {
                        displayLogIn = false;
                        System.out.print("Valid MGR ID");
                        inSystem = false;
                    } else if (content[0].equals(accountInput) & content[1].equals("Assistant")) {
                        displayLogIn = false;
                        System.out.print("Valid AST ID");
                        inSystem = false;
                    } else if (accountInput.equals("Q")) {
                        displayLogIn = false;
                        inSystem = false;
                    }
                }
            }
        }
        readEmployeesFile.close();
    }
}
import java.io.*;
导入java.util.*;
公共类帐户
{
公共静态字符串accountInput;
系统中的公共静态布尔值=true;
公共静态布尔displayLogIn=true;
公共静态扫描仪用户输入=新扫描仪(System.in);
公共静态文件employeesFile=新文件(“Employees.txt”);
公共静态void main(字符串[]args)引发IOException
{
列表内容=新建ArrayList();
System.out.println(“\t工作文件”);
System.out.println(“-----------------------------------------”;
System.out.println(“库存文件:\t\tStock.txt\n员工文件:\t\tEmployees.txt\n交易文件:\tttransactions.txt\n\n”);
扫描仪readEmployeesFile=新扫描仪(employeesFile);
while(readEmployeesFile.hasNextLine()){
字符串[]current=readEmployeesFile.nextLine().split(“,”);
内容。添加(当前);
}
while(系统内)
{
while(显示登录)
{
System.out.print(“输入员工ID(即E1)\t输入Q退出程序。\n在此处输入:”;
accountInput=userInput.nextLine();
for(字符串[]内容:内容){
if(内容[0].equals(accountInput)&内容[1].equals(“管理者”)){
displayLogIn=false;
系统输出打印(“有效的经理ID”);
inSystem=false;
}else if(内容[0].equals(accountInput)&内容[1].equals(“助手”)){
displayLogIn=false;
系统输出打印(“有效AST ID”);
inSystem=false;
}else if(accountInput.equals(“Q”)){
displayLogIn=false;
inSystem=false;
}
}
}
}
readEmployeesFile.close();
}
}
如您所见,输入文件在进入循环之前被读取一次,并将所有行存储在
内容中
,现在的类型为
列表
。在
for
循环中,您只需检查已经读取的文件内容


另一方面,您应该始终关闭正在使用的资源。在本例中,我引用了
Scanner
对象,这就是为什么我包含了行
readEmployeesFile.close()在循环之后。

当您输入一个ID时,您开始在文件上循环以查看该ID是否匹配

到达文件的末尾
readEmployeesFile.hasNextLine()
为false。再次迭代,读取下一个ID

但是,
readEmployeesFile
仍然位于文件的末尾。没有什么能让它回到起点。因此,当它再次到达
while
时,
readEmployeesFile.hasNextLine()
仍然为false

可能的解决办法:

  • 打开扫描仪,
    readEmployeesFile
    ,不是在方法的开头,而是在使用之前。然后在到达文件末尾后立即关闭它。因此,每次用户输入ID时,您都将打开和关闭它
  • 在开始提示用户之前,将文件中的所有ID读入
    映射
    ,然后在该映射中而不是直接在文件中查找ID。这样会更有效率

在Java中,两者都是有效的。您可以使用
&
&&
-区别只是短路。所以这不是OP出现问题的原因。这不是这个程序真正的问题。&'计算操作的两个方面,而不是&&'逐个计算它们。(如果第一个是真的,传递到下一个)谢谢你,但是正如另外两个所说的,这不是我代码的主要问题,当我仍然输入一个无效的ID,然后输入一个有效的ID时,它不会声明它是一个有效的ID。谢谢!我想从另外两个while循环中删除while,然后在外部执行,我从来都不知道如何执行该操作,但看过您的解决方案后,我现在就知道了!非常感谢!不客气。您应该记住,从磁盘读取文件是一项昂贵的操作,因此应尽量减少此类操作的数量。这意味着您应该只读取文件一次,然后再使用其内容。多次打开同一个文件是一个坏主意,除非它无法放入内存或者您有其他很好的理由这样做。
import java.io.*;
import java.util.*;
public class Account
{
    public static String  accountInput;
    public static boolean inSystem      = true;
    public static boolean displayLogIn  = true;
    public static Scanner userInput     = new Scanner(System.in);
    public static File employeesFile    = new File("Employees.txt");
    public static void main(String[] args) throws IOException
    {
        List<String[]> contents = new ArrayList<>();
        System.out.println("\tWorking Files");
        System.out.println("-------------------------------------------");
        System.out.println("Stock File:\t\tStock.txt\nEmployee File:\t\tEmployees.txt\nTransactions File:\tTransactions.txt\n\n");
        Scanner readEmployeesFile = new Scanner(employeesFile);
        while(readEmployeesFile.hasNextLine()) {
            String[] current = readEmployeesFile.nextLine().split(",");
            contents.add(current);
        }
        while(inSystem)
        {
            while(displayLogIn)
            {
                System.out.print("Enter Employee ID(i.e E1)\tEnter Q to Quit Program.\nEnter here: ");
                accountInput = userInput.nextLine();

                for(String[] content: contents) {
                    if (content[0].equals(accountInput) & content[1].equals("Manager")) {
                        displayLogIn = false;
                        System.out.print("Valid MGR ID");
                        inSystem = false;
                    } else if (content[0].equals(accountInput) & content[1].equals("Assistant")) {
                        displayLogIn = false;
                        System.out.print("Valid AST ID");
                        inSystem = false;
                    } else if (accountInput.equals("Q")) {
                        displayLogIn = false;
                        inSystem = false;
                    }
                }
            }
        }
        readEmployeesFile.close();
    }
}