如何实施以下内容?
我有一个LinkedList列表。 list由命令行初始化,并带有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初始化为set maxSize并传入输入文件以进行测试。
我的问题是:如何从fileName读取命令并读取密钥以便我可以执行适当的方法调用?我正在考虑使用扫描仪。我们可以假设所有命令都是一行和有效的语法。我们还可以假设maxSize足够大以反映给定的输入/输出文件。
答案 0 :(得分:0)
尝试做类似这样的事情:我使用一个非常类似的方法来完成一个项目,我必须使用链接列表和从文件中读取命令。根据需要修改。基本概念是循环,读取每个命令行,直到达到终止命令/条件,然后打印出结果或您需要为特定实例做的任何事情。
另外,这是另一个有用的链接,几乎正是您所需要的: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();