这个hashmap有什么问题,我一直在使用不兼容的类型

时间:2015-03-03 15:39:44

标签: java hashmap

//vehicle
double type[] = {0, 1.0, 1.3, 1.6, 2, 2.4, 2.7};
String carType[] = {"","Compact Car", "Small Car", "Mid Size Car", "Full Size Car", "Truck", "16 Wheeler"};
HashMap <Integer, Double> vehicle = new HashMap<Integer, Double>();
for(int y = 1; y<6; y++)
{
    vehicle.put(y, type[y]);
}
HashMap <Integer, String> CarName = new HashMap<>();
for(int a = 1; a<6; a++)
{
    CarName.put(a, carType[a]);
}

/**
 * Now begin to read the file and reference the tables. 
 */
int gate1, vehicle1, toll1, factor, cost;
String car;
System.out.println(EasyFormat.format("Car Type", 0)+EasyFormat.format("Base Toll", 15)+EasyFormat.format("Factor", 22)+EasyFormat.format("Cost",30));
while(file.hasNext())
{
    gate1 = file.nextInt();
    vehicle1 = file.nextInt();
    factor = (Double)  vehicle.get(vehicle1);
}

1 个答案:

答案 0 :(得分:4)

下面:

int factor; //other variables as well
//more code...
while(file.hasNext()) {
    //more code...
    factor = (Double)  vehicle.get(vehicle1);
}

factorint类型,您正试图向其Double发送邮件。

factor声明为double或使用Double#intValue。我希望将factor声明为double并让拆箱工作。