从命令行获取文件中的命令

Take commands from a file from command line

如何实施以下内容?

我有一个 LinkedList 列表。列表由命令行初始化并接受 2 个参数。 我们的 LinkedList 实现的命令行是

java LinkedList maxSize fileName

fileName 是一个 .txt 文件。第一个如下:

input1.txt
insert 10
Insert 15
inSert 20
delete 35
insert 35
find 12
find 35
delete 20

我们的输出是:

insert 10 true
Insert 15 true
inSert 20 true
delete 35 false
insert 35 true
find 12   false
find 35   true
delete 20 true

这意味着我们忽略字符的大小写,只关心命令插入、删除和查找。 fileName 在 LinkedList.java 中有我们对 运行 的命令(方法),并为我们提供了密钥。 commandLine 将我们的 LinkedList 初始化为一组 maxSize 并传入输入文件以进行测试。

我的问题是:如何从 fileName 中读取命令并读取密钥以便执行适当的方法调用?我正在考虑使用扫描仪。我们可以假设所有命令都是一行且语法有效。我们还可以假设 maxSize 足够大以反映给定的 input/output 个文件。

尝试做这样的事情:我对一个项目使用了非常相似的方法,我必须使用 linked-lists 并从文件中读取命令。根据需要修改。基本概念是你循环,读取每个命令行直到你到达终止 command/condition,然后打印出结果或你需要为你的特定实例做的任何事情。


此外,还有一个有用的 link,它几乎正是您所需要的:Using a text file to carry commands in Java

    boolean done = false;
    // Loop runs as long as done != true
    while (!done) {
        line = sc.nextLine();   // Store data gathered from file into String
        String [] tokens = line.split(" "); // Split the string using space as delimiter

        // Switch for processing commands received
        switch (tokens[0]) {

        // Print name followed by newline
        case "N": {
                System.out.println("Evan Clay Bechtol");
                break;
            }

        // Create a memory object of size s
        case "C": {
                memory = new Memory(Integer.parseInt(tokens[1])); // Create a new Memory object
                break;
            }

        // End of data file, print newline and exit
        case "E": {
                System.out.println();
                done = true;    // Break the loop, end the program
                break;
            }

        // Add segment of size 'u' and lifetime 'v' and print confirmation record
        case "A": {
                int size = Integer.parseInt(tokens[1]);
                int lifeTime = Integer.parseInt(tokens[2]);
                timeOfDay++;

                memory.removeSegmentsDueToDepart(timeOfDay);

                // Boolean controls whether confirmation is printed.
                while (!memory.place(size, timeOfDay, lifeTime, true)) {
                    timeOfDay++;
                    memory.removeSegmentsDueToDepart(timeOfDay);
                    } // End while
                placements++;

                // Print confirmation message
                //System.out.println("Added segment of size: " + size + "\t" + "lifeTime: " + lifeTime + "\t" + "Time of Departure: " + (lifeTime + timeOfDay));
                break;  
            }

        // Print the current segments in the list
        case "P": {
                System.out.println ();
                memory.printLayout();
                //System.out.println ("End at time: " + timeOfDay);
                break;
            }

        case "R": {
                int size = Integer.parseInt(tokens[1]); // Size
                memory = new Memory(size);
                int minSegSize = Integer.parseInt(tokens[2]);   // Minimum seg. size
                int maxSegSize = Integer.parseInt(tokens[3]);   // Maximum seg. size
                int maxLifeTime = Integer.parseInt(tokens[4]);  // Maximum lifetime of segs.
                int numSegs = Integer.parseInt(tokens[5]);      // Number of segs. to simulate
                timeOfDay = 0;
                placements = 0;
                Random ran = new Random (); // "Random" number generator

                while (placements < numSegs) {
                    timeOfDay++;
                    memory.removeSegmentsDueToDepart(timeOfDay);
                    int newSegSize = minSegSize + ran.nextInt(maxSegSize - minSegSize + 1);
                    int newSegLifetime = 1 + ran.nextInt(maxLifeTime);
                    totalSpaceTime += newSegSize * newSegLifetime;

                    while (!memory.place(newSegSize, timeOfDay, newSegLifetime, false)) {
                        timeOfDay++;
                        memory.removeSegmentsDueToDepart(timeOfDay);
                    } // End while
                    placements++;
                } // End while

                // Print final summary of execution
                meanOccupancy = totalSpaceTime / (timeOfDay);
                System.out.printf ("Number of placements made =  %6d", placements);
                System.out.println();
                System.out.printf ("Mean occupancy of memory  = %8.2f", meanOccupancy);
                System.out.println();
            }
        } // End switch
    } // End while
    sc.close();