Java 从txt文件读取到hashmap

Java 从txt文件读取到hashmap,java,hashmap,Java,Hashmap,如何从文本文件中读取字符串和int(整数)并更新HashMap中的值?文件包含: prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1])); 比托克:2 prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1])); pão(第2节):1 prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(L

如何从文本文件中读取字符串和int(整数)并更新HashMap中的值?文件包含:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
比托克:2

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
pão(第2节):1

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
布拉布拉布拉赫:1

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
以下是我到目前为止的情况:

  BufferedReader buffer = new BufferedReader(new FileReader("pedidos.txt"));

    String linha;

    int k = 1;
    String valor = null;
    try {
        while ((linha = buffer.readLine()) != null) {
            String[] Linha = linha.split(":");

            System.out.print(k + "-");
            k++;
            Linha[0] = Linha[0].trim();
            Linha[1] = Linha[1].trim();
            //prod.put(Linha[0], Integer.parseInt(Linha[1]));

            for (int i = 0; i < Linha.length; i++) {
                System.out.println(Linha[i] + " ");
                i++;
                int f = 0;
                if (f > linha.length()) {
                    System.out.println("Não existem pedidos");
                }}}
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
BufferedReader buffer=new BufferedReader(new FileReader(“pedidos.txt”);
弦琳哈;
int k=1;
字符串valor=null;
试一试{
而((linha=buffer.readLine())!=null){
字符串[]Linha=Linha.split(“:”);
系统输出打印(k+“-”);
k++;
Linha[0]=Linha[0].trim();
Linha[1]=Linha[1]。trim();
//prod.put(Linha[0],Integer.parseInt(Linha[1]);
for(int i=0;ilinha.length()){
系统输出打印(“Não existem pedidos”);
}}}
这是我的初始HashMap:

HashMap<String, Integer> prod = new HashMap<String, Integer>();
    prod.put("Queijo de Cabra", 0);
    prod.put("Queijo Fresco", 0);
    prod.put("Pão (cesta de 2)", 0);
    //PRATOS            
    prod.put("Bacalhau à Zé do Pipo", 0);
    prod.put("Bitoque", 0);
    prod.put("Salada de atum", 0);
    prod.put("Salada de Nabiças", 0);
    prod.put("Lasanha de vegetais", 0);
    //BEBIDAS            
    prod.put("Café", 0);
    prod.put("Coca-cola(lata)", 0);
    prod.put("7Up(lata)", 0);
    prod.put("Água do Luso 1.5L", 0);
    prod.put("Água do Luso ", 0);
    prod.put("Vinho da casa 1L (branco)", 0);
    prod.put("Vinho da casa 1L (tinto)", 0);
    //SOBREMESAS            
    prod.put("Bolo de bolacha", 0);
    prod.put("Banana", 0);
    prod.put("Melão", 0);
    //OUTROS            
    prod.put("Pau de canela", 0);
    prod.put("Bagaço", 0);
    prod.put("Pastilhas Gorila", 0);
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
HashMap prod=newhashmap();
产品投入(“凯霍·德卡布拉”,0);
产品投入(“奎霍壁画”,0);
产品投入(“Pão(cesta de 2)”,0);
//普瑞托斯
prod.put(“BacalhauáZédo Pipo”,0);
产品投入(“Bitoque”,0);
产品投入(“萨拉达阿图姆”,0);
产品出售(“Salada de Nabiças”,0);
产品投入(“Lasanha de vegetais”,0);
//比比达斯
产品投入(“咖啡馆”,0);
产品投入(“可口可乐(拉塔)”,0);
产品投放量(“7升(拉塔)”,0);
产品投入(“Água do Luso 1.5L”,0);
prod.put(“Água do Luso”,0);
产品投入(“Vinho da casa 1L(布兰科)”,0;
产品投入(“tinto”1升葡萄酒),0;
//索布雷马斯酒店
产品投入(“Bolo de bolacha”,0);
产品投入(“香蕉”,0);
产品投入(“Melão”,0);
//奥特罗斯
产品投入(“Pau de canela”,0);
产品出售(“Bagaço”,0);
产品投入(“Pastilhas Gorila”,0);
如何为hashmap创建类别?

查看以下内容:

import java.util.HashMap;
import java.util.Map.Entry;

public class main {

public static void main(String[] args) throws InterruptedException {
    main m = new main();
}

public void usingMaps() {
    String values = "something: 1";

    String[] parts = values.split(":");
    String key = parts[0].trim();
    Integer value = Integer.parseInt(parts[1].trim());
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    map.put(key, value);

    for (Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
}

public main() {
    usingMaps();
}
}
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));

作为另一种方法,您可以使用easy代码。因此,下面是一个简单的示例:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
test.txt

test1:1
test2:2
test3:3
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
代码:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));


编辑以模拟您拥有的代码。

Ohhh!我想我现在理解了您的问题:您在hashmap中有初始值,并且您希望通过文本文件中的值修改这些值(例如,如果文本文件说您有2个某物,您希望将该某物的计数增加2?)

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
如果这正是你想要的,那么你已经非常接近了:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
替换此项:

//prod.put(Linha[0], Integer.parseInt(Linha[1]));
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
为此:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
(这是一种快速而肮脏的方法-如果不将哈希映射中的值初始化为0,则会失败。)您可以通过测试以下条件来避免这种情况:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
if (prod.containsKey(Linha[0])) {
    // update value
    prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
} else {
    // first entry
    prod.put(Linha[0], Integer.parseInt(Linha[1]));
}

这样做的一个优点是,您可以随时添加新产品,而无需修改初始化映射的代码。

您被卡在哪里了?显示您的代码。哪些代码已经可以工作了?您可以逐个读取行吗?split是否获得您的值?您是否成功地将第二个字符串转换为整数?您是否定义了哈希映射?是否重新定义了请给我们看一些代码。如果您已经获得并分离了数据,那么您需要做的就是阅读关于如何使用HashMap.HashMap.put()的也许?我的hashmap以0开头。我需要将.txt文件中的值添加到hashmap中。这正是我所需要的。但它有一个错误:找不到符号符号:mothod contains(String)Location:variabel prod of typehashmap@user3194352oops-修复了它;应该是
containsKey
(大多数Java集合都有一个contains()方法,但映射具有containsKey和containsValue()。在您的用例中,您需要ContainesKey,因为您想知道该产品是否已经存在于您的地图中。非常感谢。您能在一件事上帮助我吗?我需要创建5个类别,并将hashmap中每个类别的值写入文件。另外,它仍然会在
prod.put(Linha[0],prod.get(Linha[0])上给我一个错误+Integer.parseInt(Linha[1]);
:线程“main”java.lang.nullpointer异常projecto.projecto.mainStack溢出不是个人的“为您做项目”“站点。如果此答案回答了您的原始问题,请接受答案并发布一个新问题,涉及如何按类别写出内容,解释您尝试过的内容和不起作用的内容,这里有人可以帮助您。如果您遇到空指针异常,请尝试打印所涉及的值(例如,在处理前打印出每一行,打印出linha[0]和[1],直到您知道该行上的哪个值为空。祝您好运!
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));