Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 堆栈顶部方法未写入.txt文件_Java - Fatal编程技术网

Java 堆栈顶部方法未写入.txt文件

Java 堆栈顶部方法未写入.txt文件,java,Java,我有一个程序,可以从.txt文件中读取购物清单。我正在使用Stack类中的方法创建一个测试驱动程序,结果打印在outputlist.txt文件中,而不是控制台中。堆栈类包括push()pop()和top()方法。我的push和pop方法工作正常,但是当我调用top方法时,它将不会打印到outputlist.txt文件中。我已经测试过该方法是否可以打印到控制台,并且效果良好。这是我在尝试使用top方法写入文件时收到的错误消息:线程“main”java.lang.ClassCastException

我有一个程序,可以从.txt文件中读取购物清单。我正在使用Stack类中的方法创建一个测试驱动程序,结果打印在outputlist.txt文件中,而不是控制台中。堆栈类包括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;

    }
}
导入java.io.FileWriter;
导入java.io.File;
导入java.util.Scanner;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.PrintWriter;
公共类测试驱动程序{
公共静态void main(字符串[]args)引发异常{
FileWriter out=null;
堆栈项目=新堆栈(“杂货清单”);
试一试{
out=newfilewriter(“outputlist.txt”,true);
File listFile=新文件(“grocerylist.txt”);
扫描仪扫描=新扫描仪(列表文件);
字符串名;
整数;
双倍价格;
while(scan.hasNextLine()){
name=scan.next();
quantity=Integer.parseInt(scan.next());
price=Double.parseDouble(scan.next());
项目推送(新杂货项目(名称、数量、价格));
}
out.write(item.toString());
对于(int i=0;i<5;i++){
item.pop();
}
写出(“5次pops后的新列表:\n”);
out.write(“^^^^^^^^^^^^^^\n”);
out.write(item.toString()+“\n”);
out.write(“顶部测试:\n”);
out.write(“^^^^^^^^^^^^^^\n”);
out.write(item.top().toString());//这就是我得到错误消息的地方。
}捕获(ioe异常ioe){
System.out.println(ioe.toString());
}
捕获(StackOverflowException soe){
out.write(“尝试在完整堆栈上推送”);
}
捕获(StackUnderflowException){
out.write(“在空堆栈上尝试弹出”);
}
out.close();
System.out.println(item.top());//这可以很好地打印到控制台。
}
公共类堆栈实现了unbounddstackinterface{
私有LinkedList;//堆栈顶部。
公共堆栈(字符串名称){
列表=新链接列表(名称);
}
公共无效推送(T元素){
列表。插入(要素);
}
公共void pop(){
list.remove();
}    
public T top()引发StackUnderflowException{
if(isEmpty())
抛出新的StackUnderflowException(“在空堆栈上尝试顶部”);
其他的
return(T)list.getLast();
}
公共布尔值为空(){
返回(list==null);
}
公共布尔值isFull(){
返回false;
}
公共字符串toString(){
return list.toString();
}   
}
公共类链接列表{
私有字符串名称;
私有节点优先;
私有LLNode-last;
私有整数大小=0;
公共链接列表(字符串名称){
第一个=空;
last=null;
this.name=名称;
}
公共空白插入(T元素){
LLNode newNode=新的LLNode(元素);
setLink(空);
if(first==null)
第一个=新节点;
if(last!=null)
last.setLink(newNode);
last=newNode;
大小++;
}    
公共整数大小(){
返回大小;
}
public void remove()引发StackUnderflowException{
节点电流;
电流=第一;
如果(大小==0)
抛出新的StackUnderflowException(“在空堆栈上尝试弹出”);
否则如果(大小==1){
last=null;
第一个=空;
尺寸=0;
}
否则{
while(current.getLink()!=last){
current=current.getLink();
}
current.setLink(空);
last=当前值;
大小--;
}                   
}
公共LLNode getLast(){
最后返回;
}        
公共字符串getName(){
返回名称;
}
公共字符串toString(){
String listString=“列表:“+name+”\n\n”;
LLNode节点;
节点=第一;
while(节点!=null){
listString=listString+node.getInfo()+“\n”;
node=node.getLink();
}
返回listString;
}
}

问题在于,在
堆栈中,您将
列表定义为
LinkedList
,其中
LinkedList
是您实现了后者的类。您的
堆栈#top
正在执行
LinkedList#getLast
getLast
返回
last
,其类型为
LLNode
,在您的例子中是
LLNode
。由于
LLNode
不是
GroceryItems
类型,因此您将获得该异常

您没有包括
LLNode
的实现,但是如果
LLNode
类中有返回
T
的方法,那么您需要调用它

因此,需要在这一行中进行更改:
out.write(项)。
return (T)list.getLast(); 
return (NodeLL<T>)list.getLast();