나는 CSV 파일을 읽고 그 내용을 해시 맵에 저장하고 해시 맵에서 특정 키의 존재를 확인하려고한다. 여기 해시 맵에 elemets를 입력하고 특정 키가 있는지 확인하는 방법은 무엇입니까?
내 코드입니다, 내가 잘못 어디에서 당신이지도를 채우는되는이outside the loop
을해야 내 실수
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);
}
}
}
당신이하고있는 것처럼 : 요소를 추가하기 위해'put', 키가 존재 하는지를 확인하기 위해'containsKey'를 사용합니다 .... 당신은 무엇을 묻고 있습니까? – assylias
아무 것도 if 조건 안에서 작동하지 않는 것 같습니다 – user1690973