[英]Read from txt file to hashmap
如何從文本文件讀取字符串和int(Integer)並更新HashMap中的值? 文件包含:
比托克:2
波城(cesta de 2):1
blahblahblah: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");
}}}
這是我最初的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);
如何為哈希圖創建類別?
看一下這個:
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();
}
}
輸出:
something
1
作為另一種方法,您可以使用簡單的代碼。 因此,簡單的示例如下所示:
test.txt
test1:1
test2:2
test3:3
碼:
Properties properties = new Properties();
properties.load(Main.class.getResourceAsStream("test.txt"));
Map map = properties;
Map<String, Integer> testMap = new HashMap<String, Integer>();
testMap.put("test0",11);
testMap.put("test00",111);
testMap.putAll(map);
System.out.println(testMap);
結果:
{test00=111, test1=1, test0=11, test2=2, test3=3}
編輯以模擬您擁有的代碼。
哦! 我想我現在已經理解了您的問題:您在哈希圖中有初始值,並且想要通過文本文件中的值來修改值(例如,如果文本文件中說您想增加2個值,則需要增加2點的東西?)
如果這就是您想要的,那么您非常親密:
替換為:
//prod.put(Linha[0], Integer.parseInt(Linha[1]));
有了這個:
prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
(這是一種快速而骯臟的方法,如果您不將哈希映射中的值初始化為0,它將失敗。)可以通過測試這種情況來避免這種情況:
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]));
}
這樣的優勢之一是您可以隨時添加新產品,而無需修改初始化地圖的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.