2012-10-27 65 views
0

我有一个程序实现了打印队列。我有一个StackClass可以找到stackjob和stackcycle,但是当我尝试打印这些值时,我会得到奇怪的结果,如hashtags。我知道我需要覆盖toString(),但是当我添加额外的代码时,我无法让我的代码编译。覆盖StackClass中的toString

我不能在我的StackClass中添加代码,因为stackjob和stackcycle在那里不存在。它们存在于我的主要方法文件PrintQueue文件中。但是,我不能在主方法中添加代码,因为我得到这个错误:PrintQ.java:10:';'预期的公共字符串toString(){^和我不能添加主方法以外的代码,因为我得到:无法找到堆栈作业和堆栈循环的符号错误。我错过了什么吗?

我确实在我的周期类相同的jobnumber可以和cycleNumber和它完美的作品:

@Override 
public String toString() 
{ 
    StringBuilder sb = new StringBuilder(); 
    sb.append(jobNumber + " "); 
    sb.append(cycleNumber); 
    return sb.toString(); 
} 

下面,stackjob和stackcycle是我需要重写的toString ..任何建议值是多少?

StackClass<Integer> stackjob = new StackClass(); 
StackClass<Integer> stackcycle = new StackClass(); 

这里是我的StackClass

 public class StackClass<T> implements StackADT<T> 
     { 
     private int maxStackSize; //variable to store the 
          //maximum stack size 
     private int stackTop;  //variable to point to 
          //the top of the stack 
     private T[] list; //array of reference variables 
    //Create an array of the size 100 to implement the stack. 
    //Postcondition: The variable list contains the base 
    //    address of the array, stackTop = 0, 
    //    and maxStackSize = 100. 
    public StackClass() 
    { 
    maxStackSize = 100; 
    stackTop = 0;   //set stackTop to 0 
    list = (T[]) new Object[maxStackSize]; //create the array 
    }//end default constructor 

    //Constructor with a parameter 
    //Create an array of the size stackSize to implement the 
    //stack. 
    //Postcondition: The variable list contains the base 
    //    address of the array, stackTop = 0, 
    //    and maxStackSize = stackSize. 
    public StackClass(int stackSize) 
    { 
    if (stackSize <= 0) 
    { 
     System.err.println("The size of the array to " 
         + "implement the stack must be " 
         + "positive."); 
     System.err.println("Creating an array of the size 100."); 

     maxStackSize = 100; 
    } 
    else 
     maxStackSize = stackSize; //set the stack size to 
            //the value specified by 
            //the parameter stackSize 
    stackTop = 0; //set stackTop to 0 
    list = (T[]) new Object[maxStackSize]; //create the array 
    }//end constructor 

    //Method to initialize the stack to an empty state. 
    //Postcondition: stackTop = 0 
    public void initializeStack() 
    { 
    for (int i = 0; i < stackTop; i++) 
     list[i] = null; 

    stackTop = 0; 
    }//end initializeStack 

    //Method to determine whether the stack is empty. 
    //Postcondition: Returns true if the stack is empty; 
    //    otherwise, returns false. 
    public boolean isEmptyStack() 
    { 
    return (stackTop == 0); 
    }//end isEmptyStack 

    //Method to determine whether the stack is full. 
    //Postcondition: Returns true if the stack is full; 
    //    otherwise, returns false. 
    public boolean isFullStack() 
    { 
    return (stackTop == maxStackSize); 
    }//end isFullStack 

    //Method to add newItem to the stack. 
    //Precondition: The stack exists and is not full. 
    //Postcondition: The stack is changed and newItem 
    //    is added to the top of stack. 
    //    If the stack is full, the method 
    //    throws StackOverflowException 
    public void push(T newItem) throws StackOverflowException 
    { 
    if (isFullStack()) 
     throw new StackOverflowException(); 

    list[stackTop] = newItem; //add newItem at the 
           //top of the stack 
    stackTop++;    //increment stackTop 
    }//end push 

    //Method to return a reference to the top element of 
    //the stack. 
    //Precondition: The stack exists and is not empty. 
    //Postcondition: If the stack is empty, the method 
    //    throws StackUnderflowException; 
    //    otherwise, a reference to the top 
    //    element of the stack is returned. 
    public T peek() throws StackUnderflowException 
    { 
    if (isEmptyStack()) 
     throw new StackUnderflowException(); 

    return (T) list[stackTop - 1]; 
    }//end peek 

    //Method to remove the top element of the stack. 
    //Precondition: The stack exists and is not empty. 
    //Postcondition: The stack is changed and the top 
    //    element is removed from the stack. 
    //    If the stack is empty, the method 
    //    throws StackUnderflowException 
    public void pop() throws StackUnderflowException 
    { 
    if (isEmptyStack()) 
     throw new StackUnderflowException(); 

    stackTop--;  //decrement stackTop 
    list[stackTop] = null; 
    }//end pop 

}

这里是我的PrintQ:

   import java.io.*; 
       import java.util.*; 

      public class PrintQ { 


    public static void main(String args[]) throws FileNotFoundException { 

    String job1; 

    int firstComma; 
    int secondComma; 

    QueueClass<Cycle> list= new QueueClass(100); 
    QueueClass<Integer> cyclelist= new QueueClass(100); 
    Cycle currentcycle= new Cycle(); 
    Cycle priorityCycle= new Cycle(); 
    Cycle Scycle= new Cycle(); 

    try{ 

     FileInputStream fstream = new FileInputStream("C:\\Users\\Whitney\\Desktop\\QueueIn.txt"); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 
     job1=br.readLine(); 
     while ((strLine = br.readLine()) != null) { 
      switch(job1.charAt(0)) { 

       case 'q': 
       { 
        System.out.println("loop q"); 
        firstComma=job1.indexOf(','); 
        secondComma=job1.lastIndexOf(','); 
        currentcycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma)); 
        currentcycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1)); 
        cyclelist.addQueue(currentcycle.cycleNumber); 
        list.addQueue(currentcycle); 

        while(currentcycle.cycleNumber > 0) 
        { 

         System.out.println(currentcycle.jobNumber + " " + currentcycle.cycleNumber); 
         currentcycle.cycleNumber--; 
        } 
        //list.print(); 
        break; 
       } 

       case 'p': 
       { System.out.println("loop priority");     
        firstComma=job1.indexOf(','); 
        secondComma=job1.lastIndexOf(','); 
        priorityCycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma)); 
        priorityCycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1)); 
        cyclelist.addQueue(priorityCycle.cycleNumber); 
        list.priorityinsert(priorityCycle); 
        while(priorityCycle.cycleNumber > 0) 
        { 
         System.out.println(priorityCycle.jobNumber + " " + priorityCycle.cycleNumber); 
         priorityCycle.cycleNumber--; 

        } 
        System.out.println(cyclelist);      
        //list.print(); 

        break; 
       } 

       case 's': 
       {System.out.println("loop s"); 
        firstComma=job1.indexOf(','); 
        Scycle.cycleNumber=Integer.parseInt(job1.substring(firstComma+1)); 
        cyclelist.addQueue(Scycle.cycleNumber);      
        list.addQueue(Scycle); 
        while(Scycle.cycleNumber > 0) 
        { 
         System.out.println(Scycle.jobNumber + " " + Scycle.cycleNumber); 
         Scycle.cycleNumber--; 

        } 


        break; 
       } 

       case 'h': 
       { 
        System.out.println("loop halt"); 
        StackClass<Integer> stackjob= new StackClass(); 
        StackClass<Integer> stackcycle= new StackClass(); 



        job1=(String) br.readLine(); 
        //list.print(); 

        System.out.println(stackjob.toString() +" " + stackcycle.toString()); 


        while((strLine = br.readLine()) != null){ 

         switch(job1.charAt(0)) { 

          case 'q': 
          { 

           firstComma=job1.indexOf(','); 
           secondComma=job1.lastIndexOf(','); 
           currentcycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma)); 
           currentcycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1)); 
           stackjob.push(currentcycle.jobNumber); 
           stackcycle.push(currentcycle.cycleNumber); 
           System.out.println("hi"); 
           System.out.println(currentcycle.jobNumber + " " + currentcycle.cycleNumber); 
           //list.print(); 
           break; 
          } 
          case 'p': 
          { 

           firstComma=job1.indexOf(','); 
           secondComma=job1.lastIndexOf(','); 
           priorityCycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma)); 
           priorityCycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1)); 
           stackjob.push(priorityCycle.jobNumber); 
           stackcycle.push(priorityCycle.cycleNumber); 
           System.out.println(priorityCycle.jobNumber-- + " " + priorityCycle.cycleNumber--); 
           break; 
          } 

          case 's': 
          { 
           firstComma=job1.indexOf(','); 
           secondComma=job1.lastIndexOf(','); 
           Scycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1)); 
           stackjob.push(0); 
           stackcycle.push(Scycle.cycleNumber); 
           System.out.println(Scycle.jobNumber + " " + Scycle.cycleNumber); 
           break; 
          } 

          case 'h': 
          { 
           System.out.println("Halt - " + list); 
           continue; 
          } 

         } 
         job1=(String) br.readLine(); 

        } 
        //System.out.println(); 

        while((stackjob.isEmptyStack()==false) || (stackcycle.isEmptyStack()==false)) 
        { 
         int printjob; 
         int printcycle; 
         Object peek; 

         printjob=stackjob.peek(); 
         printcycle=stackcycle.peek(); 
         stackcycle.pop(); 
         stackjob.pop(); 
         System.out.println("Job Number: "+printjob+" "+"Cycles Remaining: "+printcycle); 
        } 


        continue; 
       }   
      } 
      job1=br.readLine(); 
     } 
     in.close(); 
    } 
     catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 


    Cycle whilecurrent= new Cycle(); 

    while(list.isEmptyQueue()==false) 
    { 
     whilecurrent=list.front(); 
     int whilecurrentcycle= cyclelist.front(); 
     //list.print(); 
     //System.out.println(); 

     //while(whilecurrentcycle != 0) 
     //{ 
      //System.out.println();//"loop "+whilecurrentcycle--); 
     //} 
     //System.out.println(); 

     //System.out.println(whilecurrent); 

     cyclelist.deleteQueue(); 
     list.deleteQueue(); 
    } 

    list.print(); 

    } 

}

+3

什么是编译错误? –

+0

@YogendraSingh:看起来我们应该能够阅读原始海报的头脑并猜测错误。我放弃。 –

+0

嗯,我不能在我的StackClass中添加代码,因为stackjob和stackcycle不存在。它们存在于我的主要方法文件PrintQueue文件中。但是,我不能在主方法中添加代码,因为我得到这个错误:PrintQ.java:10:';'预计 公共字符串toString(){ ^ 和我不能添加主方法之外的代码,因为我得到:无法找到堆栈作业和堆栈循环的符号错误。我错过了什么吗? – wolne

回答

0

循环通过堆叠阵列,建立一字符串:

public String toString() { 
    StringBuilder output = new StringBuilder("["); 
    if (stackTop > 0) { 
    for (int i = stackTop - 1; i >= 0; i--) { 
     if (i == 0) { 
      output.append(list[i].toString()); 
     } 
     else { 
      output.append(list[i].toString() + "|"); 
     } 
    } 
    } 
    output.append("]"); 
    return output.toString(); 
} 
0

如果你想有一个快捷的解决方案,你可以随时返回列表的Arrays.toString(...):

public String toString() { 
    return java.util.Arrays.toString(list); 
} 

否则只需通过您的toString列表遍历数组方法,并将列表项返回的toString()添加到您的StringBuilder中。