.txt ファイルから買い物リストを読み込むプログラムがあります。Stack クラスにあるメソッドを使用してテスト ドライバーを作成していますが、結果はコンソールではなく outputlist.txt ファイルに出力されます。Stack クラスには、push()、pop() および top() メソッドが含まれます。push および pop メソッドは正常に動作しますが、top メソッドを呼び出すと、outputlist.txt ファイルに出力されません。メソッドがコンソールに出力され、正常に動作するかどうかを確認するためにテストしました。これは、top メソッドを使用してファイルに書き込もうとしたときに表示されるエラー メッセージです: Exception in thread "main" java.lang.ClassCastException: LLNode cannot be cast to 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;
}
}