我正在尝试阅读此输入:
processcount 2 # Read 2 processes
runfor 15 # Run for 15 time units
use rr # Can be fcfs, sjf, or rr
quantum 2 # Time quantum – only if using rr
process name P1 arrival 3 burst 5
process name P2 arrival 0 burst 9
end
我的工作是仅解析值而不是单词,并保留注释(#)。
这是主文件:
public class main {
static String[] token = new String[10];
static List<Schedule> p;
public static void schedule()
{
for(Schedule c: p)
{
System.out.println("ProcessInfo: " + c.getProcess().processName);
System.out.println("count: " + c.getProcessCount());
System.out.println("quant: " + c.getQuantum());
System.out.println("runtime: " + c.getRunTime());
System.out.println("Type: " + c.getType());
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
// sc = new Scanner(new File("processes.in"));
p = new ArrayList<>();
while(sc.hasNextLine() && !sc.equals("end"))
{
token = sc.nextLine().replace(" ","-").replace("#", "-").split("-");
System.out.println(token[0].toString());
if(!token[0].startsWith("#") || !sc.nextLine().startsWith("end"))
{
Schedule s = new Schedule();
int pCount=0, runfor=0, quantum=0, arrival=0, burst=0;
String type = null, pName = null;
if(token[0].startsWith("processcount"))
{
s.setProcessCount(Integer.parseInt(token[1]));
System.out.println(Integer.parseInt(token[1] +""));
}
else if(token[0].startsWith("runfor"))
{
s.setRunTime(Integer.valueOf(token[1].toString()));
System.out.println(Integer.parseInt(token[1]) +"");
}
else if(token[0].startsWith("use"))
{
s.setType(token[1].toString());
System.out.println(token[1] +"");
}
else if(token[0].startsWith("quantum"))
{
s.setQuantum(Integer.valueOf(token[1].toString()));
System.out.println(token[1] + "");
}
else if(token[0].startsWith("process"))
{
Processes pl = new Processes();
pl.setProcessName(token[2]);
System.out.println(token[2]+ "");
pl.setArrivalTime(Integer.valueOf(token[4].toString()));
System.out.println(""+ Integer.valueOf(token[4]));
pl.setBurstTime(Integer.valueOf(token[6].toString()));
System.out.println("" + token[6]);
s.setProcess(pl);
// add info
p.add(s);
}
else if(token[0].startsWith("end"))
{
schedule();
}
}
}
}
}
时间表如下:
public class Schedule {
int processCount;
int runTime;
String type;
int quantum;
Processes process;
public int getProcessCount() {
return processCount;
}
public void setProcessCount(int processCount) {
this.processCount = processCount;
}
public int getRunTime() {
return runTime;
}
public void setRunTime(int runTime) {
this.runTime = runTime;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getQuantum() {
return quantum;
}
public void setQuantum(int quantum) {
this.quantum = quantum;
}
public Processes getProcess() {
return process;
}
public void setProcess(Processes p) {
process = p;
}
}
这是过程:
public class Processes {
String processName;
int arrivalTime;
int burstTime;
public String getProcessName() {
return processName;
}
public void setProcessName(String processName) {
this.processName = processName;
}
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getBurstTime() {
return burstTime;
}
public void setBurstTime(int burstTime) {
this.burstTime = burstTime;
}
}
这是我的代码得到的输出:
ProcessInfo: P1
count: 0
quant: 0
runtime: 0
Type: null
ProcessInfo: P2
count: 0
quant: 0
runtime: 0
Type: null
为什么我会返回错误的结果?
最佳答案
这里有几个问题。您可以在while循环的每次迭代中创建一个新的计划;在新的迭代之前,您不会获得当前行的所有相关值,而且在创建一个新的Schedule()来覆盖先前收集的值之后,还有几个无用的变量。
另外,在数组的String元素上使用toString是没有意义的。性格方面我不会尝试不使用过滤器,并且您实际上不需要任何过滤器。总是尝试KISS(保持简单愚蠢)
这是我不使用过滤器的解决方法。
public static void main (String args [])
{
// Will help us identify the key words
String current_token;
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//Remove spaces at the beginning and end of the string
input = input.trim();
Schedule s = new Schedule();
// New source for the scanner
sc =new Scanner(input);
p = new ArrayList<>();
while(sc.hasNext())
{
current_token = sc.next();
if(current_token.equals("end"))
{schedule(); break;}
switch(current_token)
{
case "processcount":
s.setProcessCount(sc.nextInt());
System.out.println(s.getProcessCount()+ " ");
break;
case "runfor":
s.setRunTime(sc.nextInt());
System.out.println(s.getRuntime +" ");
case "use":
s.setType(sc.next());
System.out.println(s.getType() +" ");
break;
case "quantum":
s.setQuantum(sc.nextInt());
System.out.println(s.getQuantum + " ");
break;
case "process":
Processes pl = new Processes();
pl.setProcessName(sc.next());
System.out.println(pl.GetProcessName()+ " ");
pl.setArrivalTime(sc.nextInt());
System.out.println(" "+ pl.getArrivalTime());
pl.setBurstTime(sc.nextInt());
System.out.println(" " + pl.getBurstTime());
s.setProcess(pl);
// add info
p.add(s);
break;
default:
// the current_token is not what we are looking for
break;
}
}
}