Results 1 to 17 of 17
  1. #1
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default Problem extracting the values in the desired format

    Hi,

    I have created a map with a String as Key and an ArrayList of Integer values.

    The output I want is:
    Reading Applicants Numbers
    5+ Bed House : 12
    Two Bed Flat : 124 17 45 503
    One Bed Flat : 47 140 274
    3/4 Bed House : 101 97 250
    2 Bed House : 198 64

    The output I can get is:
    Reading Applicants Numbers
    5+ Bed House : [12]
    Two Bed Flat : [124, 17, 45, 503]
    One Bed Flat : [47, 140, 274]
    3/4 Bed House : [101, 97, 250]
    2 Bed House : [198, 64]

    My code to place the information in to the map is:
    Java Code:
       /**
        * Allocates the applicants to the Houses
        */
       public void makeApplications(String[] houseNames, Integer[][] applicants)
       {  
          Map<String, ArrayList<Integer>> holdMap = 
              new HashMap<String, ArrayList<Integer>>();
          for(int houseName = 0; houseName < houseNames.length; houseName++)  
          {
             ArrayList<Integer> applicant = new ArrayList<Integer>();
             for(int appNum = 0; appNum < applicants[houseName].length; appNum++)
             {
                applicant.add(applicants[houseName][appNum]);
             }
             holdMap.put(houseNames[houseName], applicant);
          }
          this.houseNameMap = holdMap;
       }
    My problem lies in that I am able to get the values for each key but am unable to 'convert' the object returned by the getKey to the format that I require - break open the object

    My current code is:
    Java Code:
       public void printTownApps()//
       {
          System.out.println(this.getTownName() + Space + "Applicants Numbers");
          for (Object house : houseNameMap.keySet())
          {
             System.out.print(house + " : " );
    //Instead of printing I need to decode the result from get(house) to a String perhaps? :smash:
             System.out.println(houseNameMap.get(house));
          }
          System.out.println();
       }
    Your help would be greatly appreciated
    GT
    Last edited by sunde887; 08-13-2011 at 11:23 AM. Reason: Code tags added, [code]...[/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The default behavior of all collections, when printed is:
    Java Code:
    public String toString() {
            Iterator<E> i = iterator();
    	if (! i.hasNext())
    	    return "[]";
    
    	StringBuilder sb = new StringBuilder();
    	sb.append('[');
    	for (;;) {
    	    E e = i.next();
    	    sb.append(e == this ? "(this Collection)" : e);
    	    if (! i.hasNext())
    		return sb.append(']').toString();
    	    sb.append(", ");
    	}
        }
    It basically loops through, but appends a [ in the front and ] at the end. To remedy this, just manually loop through the array list.

    Something like
    Java Code:
    List<Integer> numbers = houseNameMap.get(house);
    From there you can build a string with a string builder and iterating over the list.

  3. #3
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    Thanks sunde887,

    I placed your line in the print method thus:
    Java Code:
       public void printTownApps()//
       {
          System.out.println(this.getTownName() + Space + "Applicants Numbers");
          for (Object house : houseNameMap.keySet())
          {
             System.out.print(house + " : " );
             List<Integer> numbers = houseNameMap.get(house);
             System.out.println(houseNameMap.get(house));
          }
          System.out.println();
       }
    but when I compiled I got the following

    incompatible types - found java.lang.Object but expected java.util.list<java.lang.Integer>
    I think that this is because the values returned are an array of arrays of integer values.
    My test code I used to load the two methods is

    XML Code:
    String[]houseNames = {"One Bed Flat", "Two Bed Flat", "2 Bed House",
    "3/4 Bed House", "5+ Bed House"};
    Integer[][]applicants = {{47, 140, 274}, {124, 17, 45, 503},
    {198, 64}, {101, 97, 250}, {12}};
    Town Reading = new Town("Reading");
    Reading.makeApplications(houseNames, applicants);
    followed by:

    Java Code:
    Reading.printTownApps();
    HTH - GT
    Last edited by Whisperer; 08-13-2011 at 12:23 PM. Reason: Attempt to improve readability

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Which line gives that error?

  5. #5
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    The inserted line in the print routine, I just did a compile check and had not got as far as trying to build the resulting string.
    Java Code:
    List<Integer> numbers = houseNameMap.get(house);
    GT

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    What's houseNameMap defined as?

  7. #7
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    full declaration the same as holdMap in makeApplications()

    Map<String, ArrayList<Integer>> houseNameMap = new HashMap<String, ArrayList<Integer>> ();
    Last edited by Whisperer; 08-13-2011 at 01:40 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Then this:
    List<Integer> numbers = houseNameMap.get(house);
    Should be fine.
    Why are you doing this?
    Java Code:
          for (Object house : houseNameMap.keySet())
    and not
    Java Code:
          for (String house : houseNameMap.keySet())
    since the keySey is a set of Strings?

  9. #9
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    If I do use String it throws an exception

    incompatible types - found java.lang.Object but expected java.lang.String

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    That tells me that houseNameMap is not defined the way you think it is.
    That looks like its a raw type.

  11. #11
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    Would you like the complete class code, its not much longer?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    I'm about to sign off, but if you're quick.

  13. #13
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    
    public class Town
    {
       // instance variables
       private String townName;
       private Map houseNameMap;
       private String Space;
    
       /**
        * Constructor for objects of class Town
        */
       public Town(String town)
       {
          townName = town;
          Map<String, ArrayList<Integer>> houseNameMap = 
              new HashMap<String, ArrayList<Integer>>();
    //      Set the length of Space according to the length of the Town next
          Space = "         "; 
       }
    
       /**
        * Returns the name of the town
        */
       public String getTownName()
       {
          return this.townName;
       }
       
       /**
        * Allocates the applicants to the Houses
        */
       public void makeApplications(String[] houseNames, Integer[][] applicants)
       {  
          Map<String, ArrayList<Integer>> holdMap = 
              new HashMap<String, ArrayList<Integer>>();
          for(int houseName = 0; houseName < houseNames.length; houseName++)  
          {
             ArrayList<Integer> applicant = new ArrayList<Integer>();
             for(int appNum = 0; appNum < applicants[houseName].length; appNum++)
             {
                applicant.add(applicants[houseName][appNum]);
             }
             holdMap.put(houseNames[houseName], applicant);
          }
          this.houseNameMap = holdMap;
       }
    
       /**
        * Prints the applicants to the standard output device
        */
       public void printTownApps()//
       {
          System.out.println(this.getTownName() + Space + "Applicants Numbers");
          for (Object house : houseNameMap.keySet())
          {
             System.out.print(house + " : " );
     //        List<Integer> numbers = houseNameMap.get(house);
             System.out.println(houseNameMap.get(house));
          }
          System.out.println();
       }
    
    }

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    private Map houseNameMap;

    There you go.
    That should be Map<String, List<Integer>>.

  15. #15
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default

    Thanks very much having another look at the whole lot with that revision incorporated
    I suspect I shall have to change the way in which I load it in MakeApplications()
    GT

  16. #16
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Talking Resolved

    Problem solved and something simple re-learned, proper declarations of data is essential to good programming.

    Many thanks sundee and Tolls

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    There was one other thing I didn't have time to comment on:
    Java Code:
       public Town(String town)
       {
          townName = town;
          Map<String, ArrayList<Integer>> houseNameMap = 
              new HashMap<String, ArrayList<Integer>>();
    //      Set the length of Space according to the length of the Town next
          Space = "         "; 
       }
    That houseMapName there is local to the constructor and is not the same one as the one decalred earlier as a class attribute.

Similar Threads

  1. Problem in extracting the image in netbeans
    By Esther in forum NetBeans
    Replies: 4
    Last Post: 09-05-2011, 11:16 AM
  2. Extracting values from a text file
    By kryptonian03 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 06:13 AM
  3. Replies: 9
    Last Post: 03-02-2011, 11:52 PM
  4. Problem extracting items from a string with separators
    By PepsiColaMola in forum New To Java
    Replies: 3
    Last Post: 04-24-2009, 08:30 PM
  5. setting format for FLoat values
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:22 PM

Tags for this Thread

Posting Permissions

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