我正在嘗試讀取一個csv文件並將其內容存儲在散列映射中,並檢查散列映射中是否存在特定的鍵。如何在散列表中輸入元素並檢查特定鍵的存在?
這裏是我的代碼,請讓我知道我錯了,因爲讀音字螞蟻能夠找出我的錯誤
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class PoolCsv {
public static void main(String[] args) {
try {
Calendar currentdate = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
String presdate = dateformat.format(currentdate.getTime());
currentdate.add(Calendar.DAY_OF_YEAR, 4);
String futdate = dateformat.format(currentdate.getTime());
System.out.println(presdate);
System.out.println(futdate);
String poolcsv = "D:\\pool_items.csv";
BufferedReader br = new BufferedReader(new FileReader(poolcsv));
String lines = null;
String[] tokens = null;
String startdate = null;
String enddate = null;
HashMap<String, String> hash = new HashMap<String, String>();
while ((lines = br.readLine()) != null) {
tokens = lines.split(",");
for (int i = 0; i <= tokens.length; i++) {
startdate = tokens[5];
enddate = tokens[6];
}
hash.put(startdate, enddate);
boolean flag = hash.containsKey(presdate);
if (flag) {
System.out.println("value exists");
}
}
} catch (IOException io) {
System.out.println(io);
}
}
}
像你這樣做:'放''添加元素和'containsKey'來檢查一個鍵是否存在....你在問什麼? – assylias
在if條件下似乎沒有任何工作 – user1690973