java - 这个 HashMap 有什么问题,我不断收到不兼容的类型

标签 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);
}

最佳答案

这里:

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 并让拆箱完成其工作。

关于java - 这个 HashMap 有什么问题,我不断收到不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28835821/

相关文章:

java - 为什么我的 HashMap 中的值与输入的值不同?

JAVA泛型类方法不适用

java - 队列的删除方法

java - JAVA中的垃圾收集(标记-清除和引用计数)

java - 在 Java 中使用数组作为哈希键?

java - 递归或迭代地从 HashMap 中检索键值组合

java - 如何实现具有多个可能重复的字符串属性的枚举类?

java - 输出在 Java 循环的开始和结束时出现偏差

java - 持久 I/O 流连接

java - 下载和合并期间文件损坏