返回错误的值,但在系统中读得很好? Java的

时间:2016-02-19 03:17:55

标签: java

我正在尝试阅读此输入内容:

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

为什么我得到了错误的结果?

2 个答案:

答案 0 :(得分:2)

这里有几个问题。您在while循环的每次迭代中创建一个新的计划;在新迭代之前,你没有得到当前行的所有相关值,加上你创建一个覆盖以前收集的值的新Schedule()之后就有了几个无用的变量。 另外,在数组的String元素上使用toString是没有意义的。 Personnaly我不会尝试不使用过滤器,你真的不需要任何这个。总是尝试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;
           }
      }
}

答案 1 :(得分:0)

由于您拆分字符串的方式,您遇到了问题。你现在的方式首先用破折号替换每个空格。例如,字符串

"processcount 2   # a comment" 

会变成

"processcount-2---#-a-comment"

然后拆分,在每对破折号之间给你一个空字符串,这样你最终会得到

token = ["processcount", "2", "","", ... etc]

我建议你这样做:

String str = (sc.nextLine().split("#"))[0]; //get the string before the pound sign 
str = str.trim(); //remove the leading/trailing whitespace
token = str.split("\\s+"); //split the string by the whitespaces