Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在赋值后将变量添加到映射值 publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{ 双抗原; 字符串类型; 双倍价格; 设置类型=新树集(); 映射代理=新树映射(); 扫描仪控制台=新扫描仪(System.in); 字符串属性ID; 双倍总价=0; System.out.print(“请输入文件名:”); 字符串inputFileName=console.next(); 文件inputFile=新文件(inputFileName); 扫描仪输入=新扫描仪(输入文件); while(在.hasNextLine()中){ propertyID=in.next(); type=in.next(); price=in.nextDouble(); agentID=in.nextDouble(); type=type.toUpperCase(); 类型。添加(类型); if(代理包含值(agentID)){ agent.put(agentID,agents.get(agentID)+价格); } 否则{ 总价=价格; 代理。卖出(代理,总价); } } in.close(); 系统输出打印项次(类型); 系统输出打印(代理); }_Java_Map - Fatal编程技术网

Java 在赋值后将变量添加到映射值 publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{ 双抗原; 字符串类型; 双倍价格; 设置类型=新树集(); 映射代理=新树映射(); 扫描仪控制台=新扫描仪(System.in); 字符串属性ID; 双倍总价=0; System.out.print(“请输入文件名:”); 字符串inputFileName=console.next(); 文件inputFile=新文件(inputFileName); 扫描仪输入=新扫描仪(输入文件); while(在.hasNextLine()中){ propertyID=in.next(); type=in.next(); price=in.nextDouble(); agentID=in.nextDouble(); type=type.toUpperCase(); 类型。添加(类型); if(代理包含值(agentID)){ agent.put(agentID,agents.get(agentID)+价格); } 否则{ 总价=价格; 代理。卖出(代理,总价); } } in.close(); 系统输出打印项次(类型); 系统输出打印(代理); }

Java 在赋值后将变量添加到映射值 publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{ 双抗原; 字符串类型; 双倍价格; 设置类型=新树集(); 映射代理=新树映射(); 扫描仪控制台=新扫描仪(System.in); 字符串属性ID; 双倍总价=0; System.out.print(“请输入文件名:”); 字符串inputFileName=console.next(); 文件inputFile=新文件(inputFileName); 扫描仪输入=新扫描仪(输入文件); while(在.hasNextLine()中){ propertyID=in.next(); type=in.next(); price=in.nextDouble(); agentID=in.nextDouble(); type=type.toUpperCase(); 类型。添加(类型); if(代理包含值(agentID)){ agent.put(agentID,agents.get(agentID)+价格); } 否则{ 总价=价格; 代理。卖出(代理,总价); } } in.close(); 系统输出打印项次(类型); 系统输出打印(代理); },java,map,Java,Map,如果agentID中的值已包含在agents映射中,我正在尝试更新totalPrice的映射值。当我运行程序时,它将输出分配给键agentID的初始值,但不会输出totalPrice+price。我已经看了这里的问题和API文档,但我没有取得任何进展。如有任何帮助,我们将不胜感激。您似乎正在试图将代理商与价格对应起来。所以我认为你需要使用的是 public static void main(String[] args) throws FileNotFoundException { dou

如果
agentID
中的值已包含在
agents
映射中,我正在尝试更新
totalPrice
的映射值。当我运行程序时,它将输出分配给键
agentID
的初始值,但不会输出
totalPrice+price
。我已经看了这里的问题和API文档,但我没有取得任何进展。如有任何帮助,我们将不胜感激。

您似乎正在试图将代理商与价格对应起来。所以我认为你需要使用的是

public static void main(String[] args) throws FileNotFoundException {
    double agentID;
    String type;
    double price;
    Set<String> types = new TreeSet<String>();
    Map<Double, Double> agents = new TreeMap<Double, Double>();
    Scanner console = new Scanner(System.in);     
    String propertyID;
    double totalPrice = 0;

    System.out.print ("Please enter file name: ");
    String inputFileName = console.next();
    File inputFile = new File(inputFileName);
    Scanner in = new Scanner(inputFile);
    while (in.hasNextLine()) {
        propertyID = in.next();
        type = in.next(); 
        price = in.nextDouble();
        agentID = in.nextDouble();
        type = type.toUpperCase();   
        types.add(type);
        if (agents.containsValue(agentID)) {
            agents.put(agentID, agents.get(agentID)+price);
        }
        else {
            totalPrice = price;
            agents.put(agentID, totalPrice);
        }
    }
    in.close();
    System.out.println(types);
    System.out.println(agents);
}
有关更多信息,请参阅


请尽量简化问题中的代码(删除文件读取和其他不必要的信息),以便更容易确定问题可能出在哪里。

似乎您正在尝试将agentId与价格对应起来。所以我认为你需要使用的是

public static void main(String[] args) throws FileNotFoundException {
    double agentID;
    String type;
    double price;
    Set<String> types = new TreeSet<String>();
    Map<Double, Double> agents = new TreeMap<Double, Double>();
    Scanner console = new Scanner(System.in);     
    String propertyID;
    double totalPrice = 0;

    System.out.print ("Please enter file name: ");
    String inputFileName = console.next();
    File inputFile = new File(inputFileName);
    Scanner in = new Scanner(inputFile);
    while (in.hasNextLine()) {
        propertyID = in.next();
        type = in.next(); 
        price = in.nextDouble();
        agentID = in.nextDouble();
        type = type.toUpperCase();   
        types.add(type);
        if (agents.containsValue(agentID)) {
            agents.put(agentID, agents.get(agentID)+price);
        }
        else {
            totalPrice = price;
            agents.put(agentID, totalPrice);
        }
    }
    in.close();
    System.out.println(types);
    System.out.println(agents);
}
有关更多信息,请参阅


请尝试简化问题中的代码(删除文件读取和其他不必要的信息),以便更容易确定问题可能出现的位置。

您正在检查值,而应该检查地图中是否有代理

改变

if(agents.containsValue(agentID))

因为您在这里使用的是
agentID

if (agents.containsKey(agentID))

您正在检查值,而应该检查代理是否在映射中可用

改变

if(agents.containsValue(agentID))

因为您在这里使用的是
agentID

if (agents.containsKey(agentID))

在树状图中,您使用了agentID作为键totalPirce作为值,因此在代码中应该是

agents.containsKey(agentID)

不是

agent.containsValue(agentID)

问候
Isuru

在树映射中,您使用了agentID作为键totalPirce作为值,因此在代码中应该是

agents.containsKey(agentID)

不是

agent.containsValue(agentID)

问候
伊苏鲁

成功了!!!我想知道这是否是个问题,但我不知道这是一个选择。如果我有,这个问题会在2小时前得到解决。谢谢@user1401680-如果这个答案解决了你的问题,别忘了接受它。搞定它!!!我想知道这是否是个问题,但我不知道这是一个选择。如果我有,这个问题会在2小时前得到解决。谢谢@user1401680-如果这个答案解决了你的问题,别忘了接受它。