Java 从命令行从文件中获取命令
我如何实现以下内容 我有一个LinkedList列表。列表由命令行初始化,并接受2个参数。 LinkedList实现的命令行是Java 从命令行从文件中获取命令,java,file,command-line,linked-list,java.util.scanner,Java,File,Command Line,Linked List,Java.util.scanner,我如何实现以下内容 我有一个LinkedList列表。列表由命令行初始化,并接受2个参数。 LinkedList实现的命令行是 java LinkedList maxSize 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
java LinkedList maxSize 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
这意味着我们忽略字符的大小写,只关心插入、删除和查找命令。文件名包含要在LinkedList.java中运行的命令(方法),并为我们提供密钥。命令行将LinkedList初始化为设置的maxSize,并传入要测试的输入文件
我的问题是:如何从fileName读取命令并读取键,以便执行适当的方法调用?我正在考虑使用扫描仪。我们可以假设所有命令都是一行且语法有效。我们还可以假设maxSize足够大,可以反映给定的输入/输出文件。尝试这样做:我在一个项目中使用了一种非常类似的方法,我必须使用链接列表和从文件中读取命令。根据需要修改。基本概念是循环,读取每个命令行,直到达到终止命令/条件,然后打印结果或针对特定实例需要执行的任何操作
此外,这里还有一个非常有用的链接,几乎正是您所需要的:
boolean done=false;
//循环运行时间与完成时间相同!=真的
而(!完成){
line=sc.nextLine();//将从文件收集的数据存储到字符串中
String[]tokens=line.split(“”;//使用空格作为分隔符拆分字符串
//用于处理接收到的命令的开关
交换机(令牌[0]){
//打印名称后跟换行符
案例“N”:{
系统输出打印(“Evan Clay Bechtol”);
打破
}
//创建大小为s的内存对象
案例“C”:{
内存=新内存(Integer.parseInt(令牌[1]);//创建新内存对象
打破
}
//数据文件结束,打印换行符并退出
案例“E”:{
System.out.println();
done=true;//中断循环,结束程序
打破
}
//添加大小为“u”和寿命为“v”的段,并打印确认记录
案例“A”:{
int size=Integer.parseInt(标记[1]);
int lifety=Integer.parseInt(令牌[2]);
timeOfDay++;
memory.removeSegmentsDueToDepart(时间段);
//布尔值控制是否打印确认。
while(!memory.place(大小、时间、寿命、true)){
timeOfDay++;
memory.removeSegmentsDueToDepart(时间段);
}//结束时
安置++;
//打印确认信息
//System.out.println(“添加的大小段:“+size+”\t“+”生存期:“+lifety+”\t“+”出发时间:“+(生存期+timeOfDay));
打破
}
//打印列表中的当前段
案例“P”:{
System.out.println();
printLayout();
//System.out.println(“在时间结束:“+timeOfDay”);
打破
}
案例“R”:{
int size=Integer.parseInt(标记[1]);//大小
内存=新内存(大小);
int minSegSize=Integer.parseInt(标记[2]);//最小分段大小
int maxSegSize=Integer.parseInt(标记[3]);//最大seg.size
int maxlifest=Integer.parseInt(令牌[4]);//分段的最大生存期。
int numSegs=Integer.parseInt(令牌[5]);//要模拟的分段数
timeOfDay=0;
安置=0;
Random ran=新的Random();/“Random”数字生成器
while(位置
为什么不使用扫描仪?你可以将字符串转换成一个大小写来忽略大小写。是的,我在想扫描仪,但我很好奇是否有更好的方法。任务的一部分是创建最有效的算法我不知道效率;扫描仪对我来说是最简单的。当然,它也是我所知道的唯一一个。Scanner
是默认库的一部分,因此您具有可移植性优势。速度只有几毫秒不同。使用Scanner
。我想我可以使用Scanner,但我还不确定如何实现每个命令。
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();