Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In given project cityStateLocator you will find a StateCity.txt file write a code to maintain the state and city data
from file. there are multiple occurences of the same state. for example

Maharashtra-Mumbai
Maharashtra-Nagpur
Goa-panji
Karnatka-banglore

in such scenario threre should be single occurence of state as a key and multiple cities should be stored in
the list as values

Java
import java.io.*;
import java.util.*;

import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;


 
public class DataManagerImpl implements DataManager {

    public Map<string,>> populateCityDataMap(String fileName)
    throws FileNotFoundException {
        Map<string,>> mlist=new HashMap<string,>>();
        List<string> list=new ArrayList<string>();
        BufferedReader in=new BufferedReader(new FileReader(fileName));
        String str,str2;
        try {
            while((str=in.readLine())!=null){
                String[] dataArray=str.split("-");
                String masterState="";
                masterState=dataArray[0];
                //System.out.println(masterState);
                int i=0;
                if((str2=in.readLine())!=null){
                    String[] dataArray2=str2.split("-");
                    String city=dataArray2[1];
                    if(masterState.equals(dataArray2[0])){
                    	list.add(city);
                    }
                }
                i++;
                mlist.put(masterState, list);
                //list.clear();
                
            }
                
        }
        catch (IOException e) {		
            e.printStackTrace();
        }
        return mlist;
    }
        
    public List<string> getCities(Map<string,>> stateCityMap,
    String state) throws InvalidStateException {
    
        return null;
    }
Posted
Updated 17-Jul-15 7:07am
v5
Comments
Richard MacCutchan 17-Jul-15 2:57am    
Interesting, and what is the question?
PratapSinh J 17-Jul-15 7:40am    
Question is for one state there are multiple cities so, i want to store data likewise in map<string,list<string>>..
for Example
map<madhya pradesh,list<="">> then list contains citis in Madhya Pradesh only..
Richard MacCutchan 17-Jul-15 9:24am    
OK, and what is it exactly about adding names to a List that you do not understand?
Richard MacCutchan 17-Jul-15 11:07am    
but there is problem
And you think we can guess what that is?

Please do what I asked you previously:
Edit your question, and add the code, properly formatted, plus a detailed explanation of what problems you see and where in the code they occur.
phil.o 18-Jul-15 0:50am    
Please do not put code blocks in comments; they are unreadable. Better improve your question (there is a button for that) instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900