0

我有一个程序可以从 .txt 文件中读取杂货清单。我正在使用 Stack 类中的方法创建一个测试驱动程序,结果打印在 outputlist.txt 文件而不是控制台中。Stack 类包括 push() pop() 和 top() 方法。我的 push 和 pop 方法工作正常,但是当我调用我的 top 方法时,它不会打印到 outputlist.txt 文件。我已经测试过该方法是否会打印到控制台并且工作正常。这是我在尝试使用 top 方法写入文件时收到的错误消息:线程“main”java.lang.ClassCastException 中的异常:LLNode 无法转换为 GroceryItems。我已经包含了 TestDriver 类。任何意见,将不胜感激。

    import java.io.FileWriter;
    import java.io.File;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;

    public class TestDriver{

       public static void main(String[] args)throws Exception{    
         FileWriter out = null;
         Stack<GroceryItems> item = new Stack<GroceryItems>("Grocery List");

          try{
                out = new FileWriter("outputlist.txt", true);                        
                File listFile = new File("grocerylist.txt");
                Scanner scan = new Scanner(listFile);      
                String name;
                int quantity;
                double price;



                while(scan.hasNextLine()){
                   name = scan.next();  
                   quantity = Integer.parseInt(scan.next());
                   price = Double.parseDouble(scan.next());
                   item.push(new GroceryItems(name, quantity, price));                      
                }
                out.write(item.toString());

                   for(int i = 0; i < 5; i++){
                      item.pop(); 
                   }
                   out.write("New List after 5 pops:\n");
                   out.write("^^^^^^^^^^^^^^^^^^^^\n");
                   out.write(item.toString() + "\n");
                   out.write("Top test:\n");
                   out.write("^^^^^^^^^^^^^^^^^^^^\n");
                   out.write(item.top().toString()); //This is where I am getting the error message.              



          }catch (IOException ioe){
             System.out.println(ioe.toString());
          }
          catch (StackOverflowException soe){
             out.write("Push attempted on a full stack.");
          }
          catch (StackUnderflowException sue){
             out.write("Pop attempted on an empty Stack.");

          }
          out.close(); 
           System.out.println(item.top()); //This prints to console fine.       

       }
    public class Stack<T> implements UnboundedStackInterface<T>{

       private LinkedList<T> list;  //top of the stack.


       public Stack(String name){
          list = new LinkedList<T>(name);
       }
       public void push(T element){
            list.insert(element);

       }
       public void pop(){            
         list.remove();        
       }    
       public T top()throws StackUnderflowException{
           if(isEmpty())
             throw new StackUnderflowException("Top attempted on empty stack.");
           else        
          return (T)list.getLast();                            
       }
       public boolean isEmpty(){
          return (list == null);   
       }
        public boolean isFull(){
            return false;
        }
       public String toString(){            
            return list.toString();
       }   
    }


    public class LinkedList<T>{


    private String name;
    private LLNode<T> first;
    private LLNode<T> last;
    private int size = 0;


    public LinkedList(String name){
        first = null;
        last = null;        
        this.name = name;
    }
    public void insert(T element){
        LLNode<T> newNode = new LLNode<T>(element);
        newNode.setLink(null);
      if(first == null)
         first = newNode;         
      if (last != null)
         last.setLink(newNode);      
        last = newNode;
        size++;
    }    
    public int size(){
        return size;            
    }
    public void remove()throws StackUnderflowException{
        LLNode<T> current;      
        current = first;    
        if (size == 0)
            throw new StackUnderflowException("Pop attempted on an empty stack.");
        else if (size == 1){
            last = null;
            first = null;
            size = 0;
        }
        else{
            while(current.getLink() != last){                              
                current = current.getLink();                

          }
            current.setLink(null);         
            last = current;     
            size--;
        }                   


    }
   public LLNode<T> getLast(){
      return last;   

   }        
    public String getName(){
        return name;
    }
    public String toString(){
        String listString = "List: " + name + "\n\n";
        LLNode<T> node;
        node = first;

        while(node != null){
            listString = listString + node.getInfo() + "\n";
            node = node.getLink();
        }
        return listString;

    }
}
4

2 回答 2

0

问题是,在Stack<T>您定义list为时,您已实现后者的LinkedList<T>在哪里。你正在做并返回类型,在你的情况下是。由于不是类型- 你得到了那个例外。LinkedList<T>Stack#topLinkedList#getLastgetLastlastLLNode<T>LLNode<GroceryItems>LLNodeGroceryItems

您尚未包含 的实现LLNode,但如果LLNode<T>类中有返回的方法T- 那么您需要调用它。

所以需要在这一行进行更改:out.write(item.top().toString()); //This is where I am getting the error message.

于 2013-11-15T00:56:40.793 回答
0

你的getLast()回报是LLNode<T>所以当你这样做时

return (T)list.getLast(); 

您试图转换TLLNode<T>, which is wrong becauseT is not a subtype nor supertype ofLLNode . This is why you are getting aClassCastException`。

试试这个,用你的top()方法。

return (NodeLL<T>)list.getLast(); 
于 2013-11-15T00:57:27.707 回答