I have used the comparable interface before but using it with generic objects and a second object has been causing me some difficulties
Here is my driver program
import java.io.*;
import java.util.*;
public class Prog2 {
public static void main (String[]args){
//Declare Variables
Scanner inFile = null;
ListArray<Part> partArray = new ListArray<Part>(13);
//Open the file
try {
inFile = new Scanner(new File("parts.txt"));
}
//If the file is not found, end the program
catch(FileNotFoundException e){
System.out.println("Error: File not found");
System.exit(0);
}
//While the file has new text, read it in
while(inFile.hasNext()){
//Read a line of code in
String temp = inFile.nextLine();
//split the line into an array
String[] tempA = temp.split(",[ ]*");
//place the specific info into variables
int pnum = Integer.parseInt(tempA[0]);
String name = tempA[1];
double price = Double.parseDouble(tempA[2]);
String warN = tempA[3];
int quant = Integer.parseInt(tempA[4]);
//add the info into an object
partArray.add(new Part(pnum, name,price,warN,quant));
}
}
}
The class meant to be written like an Array list
public class ListArray <E extends Comparable>{
//Declare Variables
private E[] list;
private int size;
//Construct Constructor
public ListArray(){
list = (E[]) new Comparable[10];
}
public ListArray(int capacity){
list = (E[]) new Comparable[capacity];
}
/*This method will allow users to get the variable stored
* at the index they specify
* @param: int index: the index of the wanted item
* @return: E: the item at the speicifed index */
public E get(int index){
return list[index];
}
/*This method will allow users to add an element to the
* end of the list array
* @param: E item: the item being added to the array */
public void add(E item){
list[size] = item;
size++;
}
/*This mehod will allow the user to find a specified item
* inside of the array
* @param: E target: the item the user wants to know the index of
* @return: int: the index of the item found */
public int find(E target){
for(int i = 0; i < size; i++){
if(target.compareTo(list[i]) == 0){
return i;
}
}
return -1;
}
/*This method will allow users to get the size of the array
* @return: int: the size of the array */
public int size(){
return size;
}
}
and the Part class that reads in from a csv file.
public class Part <E extends Comparable>{
//Declare Variables
private int pnum;
private String name;
private double price;
private String warh;
private int quant;
//Construct Constructor
public Part(){
pnum = 0;
name = "";
price = 0.0;
warh = "";
quant = 0;
}
public Part(int pnum, String name, double price, String warh, int quant){
this.pnum = pnum;
this.name = name;
this.price = price;
this.warh = warh;
this.quant = quant;
}
//Getters
public int getPnum(){
return pnum;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String getWarh(){
return warh;
}
public int getQuant(){
return quant;
}
//Setters
public void setPnum(int pnum){
this.pnum = pnum;
}
public void setName(String name){
this.name = name;
}
public void setPrice(double price){
this.price = price;
}
public void setWarh(String warh){
this.warh = warh;
}
public void setQuant(int quant){
this.quant = quant;
}
When I run the program, I am given this error inside of the console
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray at Prog2.main(Prog2.java:8)
From the looks of it, this is a problem of how COmparable is implemented in one of my classes, and it not being correctly implemented in the other. I tried looking at other posts on the website and tried implementing them to no avail. Thank you so much!
You have specified your ListArray
to only be paramtrizable with types that extend Comparable
ListArray <E extends Comparable>
But, you're trying to parametrize it with Part
, which does not extend Comparable
.
It looks like you've made some mistake in making Part
generic. You should have Part
implement Comparable
i.e. :
public class Part implements Comparable<Part>
And then implement the compareTo
method in Part
@Override
public int compareTo(Part other) {
// ... code here
}