?
Solved

Problem accessing methods in collcetions

Posted on 2006-04-23
14
Medium Priority
?
283 Views
Last Modified: 2010-03-31
Hi I have a class which is passed a HashMap collection called BookingCollection this is assigned as BC withinbg the class
There is a method of the class that writes out to a file.

When I try to compile , it says that the method size() (or any of the collection methods ) doesnt exist

Here is a code sample

Ive omitted the actual filewrting bit

public class FileWriterBooking {
   
   
    public BookingCollection bc ;
 
    // constructor
    public FileWriterBooking(BookingCollection bookingColl)
    {  
     
     BookingCollection bc = new BookingCollection();
     bc = (BookingCollection) bookingColl ;
     
     
    }
   
    public void createFile(String fName)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy, hh:mm a");
        try
        {
           
            FileWriter bookingFile = new FileWriter(fName);
           
            PrintWriter bookingWriter = new PrintWriter(bookingFile);
             
            Booking nextBooking;
             
            int noOfBookings = bc.size();
       
0
Comment
Question by:tinybear
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 16518340
>>>      BookingCollection bc = new BookingCollection();
delete that line, it is inactive.

Where and how did you declare 'BookingCollection'???

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16518347
I bet that BookingCollection is NOT a HashMap, but contains one.
Add a method to your class that returns the size() of the internal HashMap.

BookingCollection is not a collection itself, I think.

;JOOP!
0
 

Author Comment

by:tinybear
ID: 16518352
I have a class called BookingCollection declared as follows.

public class BookingCollection
{
   
    public HashMap bookingColl;    
   
    public BookingCollection()
    {
        bookingColl = new HashMap();

This holds objects of type booking from a Bookings class
There is a cliient which instatiates the Booking Collections etc and calls the FileWriterBoooking class passing in the BookingCollection to the constructor
Hope that makes sense
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Expert Comment

by:sciuriware
ID: 16518358
Well, that's clear: BookingCollection doesn't have a 'size()', bookingColl has one.

Add a method to BookingCollection:

    public int contentSize()
    {
        return(bookingColl.size());
    }

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 400 total points
ID: 16518361
In your program:

>>> int noOfBookings = bc.size();

becomes:

   int noOfBookings = bc.contentSize();

;JOOP!
0
 

Author Comment

by:tinybear
ID: 16518372
that makes sense.

I then need to be able to access every item in the bookingColl from WITHIN the FileWriter class.

But as I had passed in bookingColl to the fileWriter I thought Id have access to it.

0
 

Author Comment

by:tinybear
ID: 16518565
so how can I access the HashMap objects from within FileWriter?

e.g i was going to use the following but that is prob not possible.

 if (noOfBookings > 0)
            {
           
                Iterator it = bc.values().iterator();
         
                while (it.hasNext())
                {
             
                    nextBooking = (Booking) it.next();
                   
                    bookingWriter.println(nextBooking.bookingRef+"\n");
                    bookingWriter.println(formatter.format(nextBooking.dateObj)+"\n");
                    bookingWriter.println(nextBooking.name+"\n");
                    bookingWriter.println(nextBooking.location+"\n");
                    bookingWriter.println(nextBooking.destination+"\n");
                    bookingWriter.println();
             
                }
               
            }
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 16518814
Provide an accessor method, then you can call:

Iterator it = bc.getBookingsMap().values().iterator();


............


public class BookingCollection
{
   
    private HashMap bookingColl;

    public HashMap getBookingsMap() {
        return bookingColl;
    }
   
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 16518828
>> so how can I access the HashMap objects

Add an accessor (setter) for the hash-map in your BookingCollection class.

public class BookingCollection
{
  ..
  public HashMap getBookingColl ()
  {
    return bookingColl ;
  }
}

Now you can iterate through it. BTW, why is the BookingCollection class needed? It seems to be nothing but a needless wrapper over a HashMap.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16518835
>>Add an accessor (setter) for the hash-map in your BookingCollection class.

Isn't that what i said  5 minutes earlier? ;-)

>>It seems to be nothing but a needless wrapper over a HashMap.

That's not necessarily the complete code perhaps...

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16518836
If you just need to access the values, you can well use a simple loop:

Object[] array = bc.getBookingColl ().values ().toArray () ;

for ( int i = 0 ; i < array.length ; i ++ )
{
  Booking nextBooking = ( Booking ) array[i] ) ;
  System.out.println ( nextBooking.bookingRef ) ;

// BTW, you should make these data-members like bookingRef private and use an accessor getBookingRef ()

}
0
 

Author Comment

by:tinybear
ID: 16518848
It is part of an overall exercise. The BookingCollection class and the Booking class were aprt of one exercise, then used again with the fileWriter & fileReader classes added in.


Ive done what you said, and all works fine now..

Many thanks, I couldnt see the wood for the trees.

Excellent repsonses all
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16518850
>> Isn't that what i said  5 minutes earlier? ;-)

Yes, CEHJ. I hope you realize the fact that I had several questions open and was posting on them.... yours wasn't on this one when I was posting mine.

>> That's not necessarily the complete code perhaps...

Correct, which is why I asked what else does the class do.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16518857
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question