Results 1 to 17 of 17
- 08-13-2011, 11:11 AM #1
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
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:
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 objectJava 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; }.gif)
My current code is:
Your help would be greatly appreciatedJava 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(); }
GTLast edited by sunde887; 08-13-2011 at 11:23 AM. Reason: Code tags added, [code]...[/code]
- 08-13-2011, 11:25 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The default behavior of all collections, when printed is:
It basically loops through, but appends a [ in the front and ] at the end. To remedy this, just manually loop through the array list.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(", "); } }
Something like
From there you can build a string with a string builder and iterating over the list.Java Code:List<Integer> numbers = houseNameMap.get(house);
- 08-13-2011, 12:15 PM #3
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
Thanks sunde887,
I placed your line in the print method thus:
but when I compiled I got the followingJava 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(); }
I think that this is because the values returned are an array of arrays of integer values.incompatible types - found java.lang.Object but expected java.util.list<java.lang.Integer>
My test code I used to load the two methods is
followed by: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);
HTH - GTJava Code:Reading.printTownApps();
Last edited by Whisperer; 08-13-2011 at 12:23 PM. Reason: Attempt to improve readability
- 08-13-2011, 01:05 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Which line gives that error?
- 08-13-2011, 01:12 PM #5
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
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.
GTJava Code:List<Integer> numbers = houseNameMap.get(house);
- 08-13-2011, 01:33 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
What's houseNameMap defined as?
- 08-13-2011, 01:38 PM #7
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
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.
- 08-13-2011, 01:45 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Then this:
List<Integer> numbers = houseNameMap.get(house);
Should be fine.
Why are you doing this?
and notJava Code:for (Object house : houseNameMap.keySet())
since the keySey is a set of Strings?Java Code:for (String house : houseNameMap.keySet())
- 08-13-2011, 01:50 PM #9
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
If I do use String it throws an exception
incompatible types - found java.lang.Object but expected java.lang.String
- 08-13-2011, 01:52 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
That tells me that houseNameMap is not defined the way you think it is.
That looks like its a raw type.
- 08-13-2011, 01:57 PM #11
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
Would you like the complete class code, its not much longer?
- 08-13-2011, 01:59 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
I'm about to sign off, but if you're quick.
- 08-13-2011, 02:00 PM #13
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
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(); } }
- 08-13-2011, 02:03 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
private Map houseNameMap;
There you go.
That should be Map<String, List<Integer>>.
- 08-13-2011, 02:06 PM #15
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
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
- 08-13-2011, 05:20 PM #16
Member
- Join Date
- Aug 2011
- Location
- Cornwall, UK
- Posts
- 22
- Rep Power
- 0
Resolved
Problem solved and something simple re-learned, proper declarations of data is essential to good programming.
Many thanks sundee and Tolls
- 08-15-2011, 10:11 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
There was one other thing I didn't have time to comment on:
That houseMapName there is local to the constructor and is not the same one as the one decalred earlier as a class attribute.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 = " "; }
Similar Threads
-
Problem in extracting the image in netbeans
By Esther in forum NetBeansReplies: 4Last Post: 09-05-2011, 11:16 AM -
Extracting values from a text file
By kryptonian03 in forum New To JavaReplies: 4Last Post: 03-22-2011, 06:13 AM -
Extracting values from textfile with semi-random content
By Kenjitsuka in forum New To JavaReplies: 9Last Post: 03-02-2011, 11:52 PM -
Problem extracting items from a string with separators
By PepsiColaMola in forum New To JavaReplies: 3Last Post: 04-24-2009, 08:30 PM -
setting format for FLoat values
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks