我有一个从.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;
}
}
答案 0 :(得分:0)
问题在于Stack<T>
您将list
定义为LinkedList<T>
,其中LinkedList<T>
是您已实施的类。您的Stack#top
正在LinkedList#getLast
和getLast
返回last
LLNode<T>
类型,在您的情况下为LLNode<GroceryItems>
。由于LLNode
不属于GroceryItems
类型,因此您将获得该异常。
您尚未包含LLNode
的实现,但如果LLNode<T>
类中的方法返回T
- 那么您需要调用它。
因此需要进行更改:out.write(item.top().toString()); //This is where I am getting the error message.
答案 1 :(得分:0)
您的getLast()
会返回LLNode<T>
所以当您这样做时
return (T)list.getLast();
您尝试将T
转换为LLNode<T>, which is wrong because
T is not a subtype nor supertype of
LLNode . This is why you are getting a
ClassCastException`。
在top()
方法中试试这个。
return (NodeLL<T>)list.getLast();