2

I know StackOver flow is not a place where you can post your homework up and ask people to do it for you but i am at my wit's end I tried simple debugging method like printing line where I can see the result, just some how the arraylist doesn't save the item when i add in

lets just say the array adds two item

  first item(name1,job1,event1,date1,location1)
  second item(name2,job2,event2,date2,location2)

The end results

name1 test234

name2 test234

name2 outloop

name2 outloop

Here is the code

public class Homework {
// ...
    private int cin;
    private String[] jobarray;
    private String[] eventarray;
    private Timetable tb;
    private String[] namesarray;
//...

    public ArrayList retrievebyname(String name, String date) {
        ResultSet rs = null;
        DBController db = new DBController();
        ArrayList<Timetable> list = new ArrayList();

        // start of for loop**
        for (int k = 0; k < cin; k++) {
            // step 1 - establish connection to database
            db.getConnection();

            // step 2 - declare the SQL statement
            String dbQuery = "select event.eventName,Timetable.location,Timetable.date "
                    + "From Timetable " + "inner join event "
                    + "on event.eventId=Timetable.eventId " + "inner join VolunteerJob "
                    + "on VolunteerJob.ID= Timetable.jobId " + "Where JobName='"
                    + jobarray[k] + "'" + "and Timetable.date ='" + date + "'"
                    + "and eventName='" + eventarray[k] + "'";
            // step 3 - to retrieve data using readRequest method
            rs = db.readRequest(dbQuery);

            try {
                if (rs.next()) {
                    tb.setName(namesarray[k]);
                    tb.setJobName(jobarray[k]);
                    tb.setEventName(rs.getString("eventName"));
                    tb.setDate(rs.getString("date"));
                    tb.setLocation(rs.getString("location"));
                    // **Adding the item retrieve into ArrayList called List**
                    list.add(k, tb);

                    System.out.println(list.get(k).getName() + " test234");
                }
            } catch (Exception e) {
                e.printStackTrace(); // fail to retrieve, print error message
            }

            // step 4 - close connection
            db.terminate();
        } // end of for loop**
        System.out.println(list.get(0).getName() + " outloop");
        System.out.println(list.get(1).getName() + " outloop");

        return list;
    }
}
4
  • 3
    Actually you may post your homework questions and people will try to help you. Just don't expect to receive finished answers that relieve you of the learning burden. Commented Aug 2, 2014 at 14:00
  • Try using list.add(tb); instead. Commented Aug 2, 2014 at 14:02
  • i tried list.add(tb) i got the same result so i thought adding the position would be better Commented Aug 2, 2014 at 14:04
  • Because you've clearly put some effort into this, the folks here will be happy to help. Commented Aug 2, 2014 at 14:06

1 Answer 1

4

After your edit, I see that you created one instance of Timetable at the start of the method. That's not enough. It means you are adding the same object to the list over and over again, and you are overwriting its properties in each iteration.

You must create a new instance of Timetable for each object you add to the list.

    tb = new Timetable ();
    tb.setName(namesarray[k]);
    tb.setJobName(jobarray[k]); 
    tb.setEventName(rs.getString("eventName")); 
    tb.setDate(rs.getString("date"));
    tb.setLocation(rs.getString("location"));
    list.add(k, tb)
Sign up to request clarification or add additional context in comments.

1 Comment

wow thanks it works already actually I did added in the program but I added it outside of the Loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.