How to enter elemets in a hashmap and check for the existence of a particular key?

Ads
 

How to enter elemets in a hashmap and check for the existence of a particular key?

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);
              }
    }


}
View Answers

Ads









Related Tutorials/Questions & Answers:
How to enter elemets in a hashmap and check for the existence of a particular key?
how to check particular value is avilable in hashmap - Java Beginners
Advertisements
How to get Keys and Values from HashMap in Java?
Problem while using a HashMap for writing text ina RTF file. - Framework
Checking Directory existence on Ftp Server
Checking File existence on Ftp Server
Java file exist
to enter into a particular page only if the username,password and listbox value mtches
Mysql Check
HashMap
how to create customise shortcut keys in jsp?
How to check if a string exits in a string script in Java script?
How to handle enter key in javascript
how to insert check box
Hashmap
Check for any elements exists between the current position and the limit of a buffer.
Check for number of elements exists between the current position and the limit of a buffer.
HashMap
how to check a remote applications validity
hashmap
Check for any elements exists between the current position and the limit of a double type buffer.
Check for number of elements exists between the current position and the limit of a double type buffer.
Search file on Ftp Server
How to check a checkbox - Struts
How to check spark version
How to check if a pid is alive?
HASHMAP
how to sort the elements of hashmap and to print output the key and the value attached to it
How to check a file exists or not in java.
Hashmap
How to know the Sunrise time of particular date and Location.

Ads