Writing a tracklist class
I am trying to write a tracklist class. It basically stores tracks for a CD as Strings. I think my code is okay. The only problem is the add method.
Code:
class tracklist {
//define variables
CD[] a;
int numElements;
//no arg constructer
tracklist() {
a = new CD[100];
numElements = 0;
}
public boolean add(String track) {
if (numElements <= 100) {
a[numElements] = track;
numElements++;
return true;
} else {
return false;
}
}
public int count() {
return numElements;
}
public void display(int indent){
for (int i = 1; i <= 100; i++){
System.out.println(i + " " + a[i]);
}
}
}
I'm trying to get my code to compile but I get the following error message:
Quote:
Assignment3.java:62: incompatible types
found : java.lang.String
required: assignment3.CD
a[numElements] = track;
^
1 error
How would I fix this??
Re: Writing a tracklist class
Your array contains CD type elements; you are trying to store a String type element in there (that's what your compiler said). For now (I think) changing your array a to a String[] will do. b.t.w. why are you trying to display 100 elements when only 'numElements' are stored in your array?
kind regards,
Jos
Re: Writing a tracklist class
Thanks, I changed the array to a String[] and got it to compile. I have no idea why I was trying to display 100 elements. I didn't notice that. I'll fix that part up.