Rick Stevens Rick Stevens - 4 years ago 144
Java Question

Incompatible type: List<> cannot be converted to float

I've got an error and I can't find out why. I'm loading an excel file that is use to retrieve information about a product.

The error I get is at the following line:
productListFinal = new HashMap< Integer, List< DatabaseItems>>(id, databaseItems);
The error says: incompatible types: DatabaseItems cannot be converted to float. I don't even work with float types so I don't get it where this error came from.

DatabaseLoader.java

public class DatabaseLoader {

public final String FILE_PATH = "" + System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "main" + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "database" + System.getProperty("file.separator") + "DatabaseFull.xlsx";

public Map LoadDatabase() {
List<DatabaseItems> productList = new LinkedList();
Map<Integer, List<DatabaseItems>> productListFinal = new HashMap<>();
FileInputStream fis = null;
try {
fis = new FileInputStream(FILE_PATH);
Workbook workbook = new XSSFWorkbook(fis);

int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = workbook.getSheetAt(i);
Iterator rowIterator = sheet.iterator();

while (rowIterator.hasNext()) {
//DatabaseItems databaseItems = new DatabaseItems();
Row row = (Row) rowIterator.next();
if (row.getRowNum() == 0) {
continue; //just skip the rows if row number is 0 or 1
} else {
}
if (row.getCell(0, Row.RETURN_BLANK_AS_NULL) == null) {
break;
} else {
Integer id = readInteger(row.getCell(0, Row.RETURN_BLANK_AS_NULL));
String firstName = readString(row.getCell(1, Row.RETURN_BLANK_AS_NULL));
String secondName = readString(row.getCell(2, Row.RETURN_BLANK_AS_NULL));
String description = readString(row.getCell(3, Row.RETURN_BLANK_AS_NULL));
String content = readString(row.getCell(4, Row.RETURN_BLANK_AS_NULL));
String barcode = readString(row.getCell(5, Row.RETURN_BLANK_AS_NULL));
String temperature = readString(row.getCell(6, Row.RETURN_BLANK_AS_NULL));
String dateDescription = readString(row.getCell(7, Row.RETURN_BLANK_AS_NULL));
Integer dateOffset = readInteger(row.getCell(8, Row.RETURN_BLANK_AS_NULL));
String price = readString(row.getCell(9, Row.RETURN_BLANK_AS_NULL));
String ingredients = readString(row.getCell(10, Row.RETURN_BLANK_AS_NULL));
String alergenes = readString(row.getCell(11, Row.RETURN_BLANK_AS_NULL));
String energykj = readString(row.getCell(12, Row.RETURN_BLANK_AS_NULL));
String energykcal = readString(row.getCell(13, Row.RETURN_BLANK_AS_NULL));
String protein = readString(row.getCell(14, Row.RETURN_BLANK_AS_NULL));
String carbohydrate = readString(row.getCell(15, Row.RETURN_BLANK_AS_NULL));
String sugar = readString(row.getCell(16, Row.RETURN_BLANK_AS_NULL));
String polyool = readString(row.getCell(17, Row.RETURN_BLANK_AS_NULL));
String starch = readString(row.getCell(18, Row.RETURN_BLANK_AS_NULL));
String fat = readString(row.getCell(19, Row.RETURN_BLANK_AS_NULL));
String saturated = readString(row.getCell(20, Row.RETURN_BLANK_AS_NULL));
String singlefat = readString(row.getCell(21, Row.RETURN_BLANK_AS_NULL));
String multiplefat = readString(row.getCell(22, Row.RETURN_BLANK_AS_NULL));
String vegetablefat = readString(row.getCell(23, Row.RETURN_BLANK_AS_NULL));
String animalfat = readString(row.getCell(24, Row.RETURN_BLANK_AS_NULL));
String nutritivevalue = readString(row.getCell(25, Row.RETURN_BLANK_AS_NULL));
String vegetable = readString(row.getCell(26, Row.RETURN_BLANK_AS_NULL));
String animal = readString(row.getCell(27, Row.RETURN_BLANK_AS_NULL));
String mineral = readString(row.getCell(28, Row.RETURN_BLANK_AS_NULL));
String sodium = readString(row.getCell(29, Row.RETURN_BLANK_AS_NULL));
String salt = readString(row.getCell(30, Row.RETURN_BLANK_AS_NULL));
String moisture = readString(row.getCell(31, Row.RETURN_BLANK_AS_NULL));
String alcohol = readString(row.getCell(32, Row.RETURN_BLANK_AS_NULL));

DatabaseItems databaseItems = new DatabaseItems(id, firstName, secondName, description, content, barcode, temperature,
dateDescription, dateOffset, price, ingredients, alergenes, energykj, energykcal, protein, carbohydrate, sugar, polyool,
starch, fat, saturated, singlefat, multiplefat, vegetablefat, animalfat, nutritivevalue, vegetable, animal, mineral, sodium, salt, moisture, alcohol);
productList.add(databaseItems);

productListFinal = new HashMap<Integer, List<DatabaseItems>>(id, databaseItems);
System.out.println("databaseItems: ");
System.out.println("productList: " + productList);

}
}
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return productListFinal;
}
public Integer readInteger(Cell cell) {
Integer value;
if (cell == null) {
value = 0;
} else {
switch (cell.getCellTypeEnum()) {
case NUMERIC:
value = (int) Math.round(cell.getNumericCellValue());
break;
case STRING:
value = Integer.parseInt(cell.getStringCellValue());
break;
case BOOLEAN:
value = Integer.parseInt(String.valueOf(cell.getBooleanCellValue()));
break;
default:
value = Integer.parseInt(cell.getStringCellValue());
}
}
return value;
}

}


The DatabaseItems.java

public class DatabaseItems {

private Integer id;
private String fname;
private String sname;
private String description;
private String content;
private String barcode;
private String temperature;
private String dateDescription;
private Integer dateOffset;
private String price;
private String ingredients;
private String alergenes;
private String energykj;
private String energykcal;
private String protein;
private String carbohydrate;
private String sugar;
private String polyool;
private String starch;
private String fat;
private String saturated;
private String singlefat;
private String multiplefat;
private String vegetablefat;
private String animalfat;
private String nutritivevalue;
private String vegetable;
private String animal;
private String mineral;
private String sodium;
private String salt;
private String moisture;
private String alcohol;

public DatabaseItems(Integer id, String fname, String sname, String description, String content, String barcode, String temperature, String dateDescription, Integer dateOffset, String price, String ingredients, String alergenes, String energykj, String energykcal,
String protein, String carbohydrate, String sugar, String polyool, String starch, String fat, String saturated, String singlefat, String multiplefat,
String vegetablefat, String animalfat, String nutritivevalue, String vegetable, String animal, String mineral, String sodium, String salt, String moisture, String alcohol) {
this.id = id;
this.fname = fname;
this.sname = sname;
this.description = description;
this.content = content;
this.barcode = barcode;
this.temperature = temperature;
this.dateDescription = dateDescription;
this.dateOffset = dateOffset;
this.price = price;
this.ingredients = ingredients;
this.alergenes = alergenes;
this.energykj = energykj;
this.energykcal = energykcal;
this.protein = protein;
this.carbohydrate = carbohydrate;
this.sugar = sugar;
this.polyool = polyool;
this.starch = starch;
this.fat = fat;
this.saturated = saturated;
this.singlefat = singlefat;
this.multiplefat = multiplefat;
this.vegetablefat = vegetablefat;
this.animalfat = animalfat;
this.nutritivevalue = nutritivevalue;
this.vegetable = vegetable;
this.animal = animal;
this.mineral = mineral;
this.salt = sodium;
this.salt = salt;
this.moisture = moisture;
this.alcohol = alcohol;
}

public Integer getId() {
return id;
}

public String getFirstName() {
return fname;
}

public String getSecondName() {
return sname;
}

public String getDescription() {
return description;
}

public String getContent() {
return content;
}

public String getBarcode() {
return barcode;
}

public String getTemperature() {
return temperature;
}

public String getDateDescription() {
return dateDescription;
}

public Integer getDateOffset() {
return dateOffset;
}

public String getPrice() {
return price;
}

public String getIngredients() {
return ingredients;
}

public String getAlergenes() {
return alergenes;
}

public String getEnergykj() {
return energykj;
}

public String getEnergykcal() {
return energykcal;
}

public String getProtein() {
return protein;
}

public String getCarbohydrate() {
return carbohydrate;
}

public String getSugar() {
return sugar;
}

public String getPolyool() {
return polyool;
}

public String getStarch() {
return starch;
}

public String getFat() {
return fat;
}

public String getSaturated() {
return saturated;
}

public String getSinglefat() {
return singlefat;
}

public String getMultiplefat() {
return multiplefat;
}

public String getVegetablefat() {
return vegetablefat;
}

public String getAnimalfat() {
return animalfat;
}

public String getNutritivevalue() {
return nutritivevalue;
}

public String getVegetable() {
return vegetable;
}

public String getAnimal() {
return animal;
}

public String getMineral() {
return mineral;
}

public String getSodium() {
return sodium;
}

public String getSalt() {
return salt;
}

public String getMoisture() {
return moisture;
}

public String getAlcohol() {
return alcohol;
}

public void setId(Integer id) {
this.id = id;
}

public void setFirstName(String fname) {
this.fname = fname;
}

public void setSecondName(String sname) {
this.sname = sname;
}

public void setDescription(String description) {
this.description = description;
}

public void setContent(String content) {
this.content = content;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}

public void setTemperature(String temperature) {
this.temperature = temperature;
}

public void setDateDescription(String dateDescription) {
this.dateDescription = dateDescription;
}

public void setDateOffset(Integer dateOffset) {
this.dateOffset = dateOffset;
}

public void setPrice(String price) {
this.price = price;
}

public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}

public void setAlergenes(String alergenes) {
this.alergenes = alergenes;
}

public void setEnergykj(String energykj) {
this.energykj = energykj;
}

public void setEnergykcal(String energykcal) {
this.energykcal = energykcal;
}

public void setProtein(String protein) {
this.protein = protein;
}

public void setCarbohydrate(String carbohydrate) {
this.carbohydrate = carbohydrate;
}

public void setSugar(String sugar) {
this.sugar = sugar;
}

public void setPolyool(String polyool) {
this.polyool = polyool;
}

public void setStarch(String starch) {
this.starch = starch;
}

public void setFat(String fat) {
this.fat = fat;
}

public void setSaturated(String saturated) {
this.saturated = saturated;
}

public void setSinglefat(String singlefat) {
this.singlefat = singlefat;
}

public void setMultiplefat(String multiplefat) {
this.multiplefat = multiplefat;
}

public void setVegetablefat(String vegetablefat) {
this.vegetablefat = vegetablefat;
}

public void setAnimalfat(String animalfat) {
this.animalfat = animalfat;
}

public void setNutritivevalue(String nutritivevalue) {
this.nutritivevalue = nutritivevalue;
}

public void setVegetable(String vegetable) {
this.vegetable = vegetable;
}

public void setAnimal(String animal) {
this.animal = animal;
}

public void setMineral(String mineral) {
this.mineral = mineral;
}

public void setSodium(String sodium) {
this.sodium = sodium;
}

public void setSalt(String salt) {
this.salt = salt;
}

public void setMoisture(String moisture) {
this.moisture = moisture;
}

public void setAlcohol(String alcohol) {
this.alcohol = alcohol;
}

@Override
public String toString() {
return "DatabaseItems{" + "id=" + id + ", fname=" + fname + ", sname=" + sname + ", description=" + description + ", content=" + content + ", barcode=" + barcode + ", temperature=" + temperature + ", dateDescription=" + dateDescription + ", dateOffset=" + dateOffset + ", price=" + price + ", ingredients=" + ingredients + ", alergenes=" + alergenes + ", energykj=" + energykj + ", energykcal=" + energykcal + ", protein=" + protein + ", carbohydrate=" + carbohydrate + ", sugar=" + sugar + ", polyool=" + polyool + ", starch=" + starch + ", fat=" + fat + ", saturated=" + saturated + ", singlefat=" + singlefat + ", multiplefat=" + multiplefat + ", vegetablefat=" + vegetablefat + ", animalfat=" + animalfat + ", nutritivevalue=" + nutritivevalue + ", vegetable=" + vegetable + ", animal=" + animal + ", mineral=" + mineral + ", sodium=" + sodium + ", salt=" + salt + ", moisture=" + moisture + ", alcohol=" + alcohol + '}';
}

}

Answer Source

You are passing the wrong parameters to HashMap constructor. The only constructor which accepts two arguments is this one:

HashMap(int initialCapacity, float loadFactor)

The arguments you sent doesn't meet the signature. According to Java docs:

Constructs an empty HashMap with the specified initial capacity and load factor.

I assume that you are trying to create a map with id entry with a shortcut, which doesn't works :)

Try to create a HashMap first, them put the entry in a separate command.

productListFinal = new HashMap<Integer, List<DatabaseItems>>();
productListFinal.put(id, databaseItems);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download