Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to enter elemets in a hashmap and check for the existence of a particular key?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


arpita
How to enter elemets in a hashmap and check for the existence of a particular key?
0 Answer(s)      2 years and a month ago
Posted in : Java Beginners


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


}

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com