Results 1 to 7 of 7
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default HashMap problem...

    Hello ... i am working about HashMap and ReadWrite File system...

    i have this input.txt file :

    Pete San Francisco
    Maria Boston
    Dimitris Thessaloniki
    Dimitris Heraklion
    Pete Toronto
    George Barcelona
    Aris Rome
    Aris Boston
    Dimitris Ioannina
    Maria Helsinki
    Anna London
    Pete Rome
    Anna Paris
    Anna Athens
    Pete Ioannina
    Anna Rio De Janeiro
    George Helsinki
    George New York

    this file has the names and the cities of the people which they have gone.. i want to make a program which will take this info and will create a result.txt file with this results :


    pete San Francisco , Toronto , Rome , Ioannina
    Maria Boston , Helsinki
    Dimitris Thessaloniki , Heraklion , Ioannina
    George Barcelona , Helsinki , New York
    Aris Rome , Boston
    Anna London , Paris , Athens , Rio De Janeiro

    the name must have a tab distance with the cities list.. and i don't care about the order of name or cities..

    here is what i have done :

    Java Code:
    import java.util.Scanner;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.io.PrintWriter;
    import java.util.Iterator;
    
    class ReadWriteTest{
    	public static void main(String args[]){
    		HashMap<String,ArrayList<String>> myMap = new HashMap<String,ArrayList<String>>();
    		Scanner inputReader = null;
    		try {
    			inputReader = new Scanner(new FileInputStream("input.txt"));
    		}
    		catch(FileNotFoundException e){
    			System.out.println("File not Found..");
    			System.exit(0);
    		}
    		String[] tokens;
    		String line;
    		ArrayList<String> list;
    		while(inputReader.hasNextLine()){
    			line = inputReader.nextLine();
    			tokens = line.split("\t");
    			if(myMap.containsKey(tokens[0])){
    				list = myMap.get(tokens[0]);
    				list.add(tokens[1]);
    			}else{
    				list = new ArrayList<String>();
    				list.add(tokens[1]);
    				myMap.put(tokens[0],list);
    			}
    			line = inputReader.nextLine();
    		}
    		inputReader.close();
    		PrintWriter outputWriter = null;
    		try{
    			outputWriter = new PrintWriter(new FileOutputStream("results.txt",true));
    		}
    		catch(FileNotFoundException e){
    			System.out.println("Error opening the file stuff.txt.");
    			System.exit(0);
    		}
    		outputWriter.println();
    		Iterator iterator = myMap.keySet().iterator();  
    		line = "";
    		while (iterator.hasNext()) {  
    			String key = iterator.next().toString();  
    			list = myMap.get(key);
    			
    			for(int i = 0 ; i < list.size() ; i ++){
    				line = line + list.get(i) + " , "; 
    			}
    			
    			outputWriter.println(key + "	" + line);  
    		}  
    		outputWriter.close();
    	}
    }
    and when i run it i got this results :


    Dimitris Thessaloniki , Ioannina ,
    Pete Thessaloniki , Ioannina , San Francisco , Toronto , Ioannina ,
    Aris Thessaloniki , Ioannina , San Francisco , Toronto , Ioannina , Rome ,
    George Thessaloniki , Ioannina , San Francisco , Toronto , Ioannina , Rome , Helsinki ,
    Anna Thessaloniki , Ioannina , San Francisco , Toronto , Ioannina , Rome , Helsinki , London , Paris ,

    what i have done wrong?!?!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: HashMap problem...

    You need to reset your line for each iteration in the output loop.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: HashMap problem...

    hmmmm right!! i should add the line 48 inside the for loop...but still i got this output :

    Dimitris Thessaloniki , Ioannina ,
    Pete San Francisco , Toronto , Ioannina ,
    Aris Rome ,
    George Helsinki ,
    Anna London , Paris ,

    if you see the results here and the results i want to have it still missing some cities.. but i don't understand why!... :(


    i am going to check if the mistake is inside the hashMap or in print code..
    Last edited by ShadowWalker; 05-20-2013 at 10:08 PM.

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: HashMap problem...

    i just add a System.out.println(tokens[1]); in line 27 ... if i have right it should print all the cities right?!?!
    but when i run it i got this :

    San Francisco
    Thessaloniki
    Toronto
    Rome
    Ioannina
    London
    Paris
    Ioannina
    Helsinki

    Still some cities is missing.. for example New York or Boston ... but the questions is why...

    i think that i have found the problem.. for some reason i didn't read all lines..maybe the while condition is not right.. i should check it again
    Last edited by ShadowWalker; 05-20-2013 at 10:19 PM.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: HashMap problem...

    It also looks like you are skipping every other line (two nextLine() calls in the read loop).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: HashMap problem...

    Omg!!! how of the... i didn't saw that!!! Thx you very much... i really appreciate your help!

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: HashMap problem...

    You are most Welcome!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. HashMap problem
    By tcms in forum New To Java
    Replies: 12
    Last Post: 05-17-2013, 09:37 AM
  2. HashMap problem
    By Java Junior in forum Advanced Java
    Replies: 6
    Last Post: 03-16-2012, 06:03 PM
  3. HashMap problem
    By vince8864 in forum New To Java
    Replies: 1
    Last Post: 02-13-2012, 11:51 AM
  4. hashmap problem
    By minotaurus in forum Advanced Java
    Replies: 5
    Last Post: 03-16-2011, 11:24 AM
  5. Problem with HashMap
    By maz09 in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •