Hi, I am trying to read a csv file and storing its contents in a hash map and checking the existence of a particular key in the hash map.
Here is my code, Please let me know where am i wrong because i m ant able to figure out my mistake:
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); } } }
Ads