我有一个LinkedList类,我不允许为我的CS类更改。我还有一个程序,其中包含我的主要方法和一个名为Book的类,它可以存储有关书籍的信息(ISBN,价格,库存)。我的主要方法是将书籍放入链表。我虽然无法访问列表中的数据。
static void handleBooks(String Books,LinkedList BookInv)throws FileNotFoundException{
File inputFile = new File(Books);
Scanner input = new Scanner(inputFile);
while(input.hasNextLine()){
Scanner line = new Scanner(input.nextLine());
BookInv.add(new Book(line.next(), Double.parseDouble(line.next()), Integer.parseInt(line.next())));
}
//System.out.println(BookInv.front.next.data.ISBN);
}
注释掉的线是我试图开始工作的线。关于一本书的数据也来自一个文件。
编辑:
public class LinkedList<E>{
node front;
int size;
public class node {
E data;
node next;
node(E dataNext){
data = dataNext;
next = null;
}
node(E data, node next){
this.data = data;
this.next = next;
}
void setData(E dataValue) {
data = dataValue;
}
node getNext() {
return next;
}
void setNext(node nextValue) {
next = nextValue;
}
}
public void append(E data){
if (front == null){
front = new node(data);
} else {
node curr = front;
while (curr.next != null){
curr = curr.next;
}
curr.next = new node(data);
}
size++;
}
public void prepend(E data){
front = new node(data,front);
size++;
}
public LinkedList(){
front = null;
}//Empty linkedlist
public int size(){
return size;
}
boolean isEmpty(){
return front == null;
}
public void add(E Value){
if(front == null){
front = new node(Value);
}else{
node current = front;
while(current.next !=null){
current = current.next;
}
current.next = new node(Value);
}
size++;
}
public void add(int index, E value){
if (index == 0){
prepend(value);
} else {
node curr = front;
for (int i = 0; i < index-1; i++){
curr = curr.next;
}
curr.next = new node(value,curr.next);
size++;
}
}
public E get(int index){
if(isEmpty()){
throw new IndexOutOfBoundsException();
}
node curr = front;
//if 0 < index < size
for(int i = 0; i < index; i++){
curr = curr.next;
}
return curr.data;
}
public E remove(){
if(isEmpty()){
throw new IndexOutOfBoundsException();
}
E temp = front.data;
front = front.next;
size--;
return temp;
}
public E remove(int index){
E temp;
if (index < 0 || index >= size){
return null;
}
//special cases
if (index == 0){
temp = front.data;
front = front.next;
} else {
//normal case
node curr = front;
for (int i = 0; i < index-1; i++){
curr = curr.next;
}
temp = curr.next.data;
curr.next = curr.next.next;
}
size--;
return temp;
}
}
收到错误消息“BookInventory.java:49:错误:找不到符号符号:变量ISBN位置:E类型的可变数据