Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: Java: Muuttujaan kertyy väärää tietoa sisäkkäisissä silmukoissa

walkout_ [22.11.2016 11:08:09]

#

Hei,

Minulla on ongelma luupata List- ja Collection-luokat:

while (iterator.hasNext()) {

            	Issue issueIter = (Issue) iterator.next();

            	Status status = issueIter.getStatusObject();
//// koodia
Collection<Issue> subTasks = issueIter.getSubTaskObjects();

						System.out.println(subTasks.size());

						if (subTasks.size() == 0) {

						obj = new JSONObject()
						.put("Id", issueIter.getId())
						.put("Name",issueIter.getKey()+": "+issueIter.getSummary())
						.put("PercentDone",percentDone)
						.put("leaf",true)
						//.put("BaselineStartDate",createddate.toString())
						.put("StartDate",createddate.toString())
						.put("EndDate",due.toString());
						//.put("BaselineEndDate",due.toString());

						arr.put(obj);

						} else {

					    for (Iterator subTasksIterator=subTasks.iterator(); subTasksIterator.hasNext();) {
     						Issue subtask = (Issue) subTasksIterator.next();

							if (subtask.getOriginalEstimate()==null) {
								orgEstSub = 0.0;
								doubleOrgEstSub = Double.valueOf(orgEstSub);
								percentOrgEstSub = 0;
							} else {
								orgEstSub = Double.valueOf(subtask.getOriginalEstimate());
								doubleOrgEstSub = Double.valueOf(orgEstSub / 3600);
								percentOrgEstSub = subtask.getOriginalEstimate();
							}

							if (subtask.getEstimate()==null) {
								remEstSub = 0.0;
								doubleRemEstSub = Double.valueOf(remEstSub);
							} else {
								remEstSub = Double.valueOf(subtask.getEstimate());
								doubleRemEstSub = Double.valueOf(remEstSub / 3600);
							}

							if (subtask.getTimeSpent()==null || doubleOrgEstSub==0.0) {
        	    	        gtsSub = 0;
        	    	        percentDoneSub = 0;
        	                } else {
        	    	        gtsSub = subtask.getTimeSpent();
        	    	        percentDoneSub = gtsSub * 100 / percentOrgEstSub;
        	                }

							if (subtask.getDueDate()==null) {
								enddateSub = subtask.getCreated();
								duedateSub = enddateSub.getTime() + 1 * 24 * 60 * 60 * 1000;
								dueSub = new Timestamp(duedateSub);
							} else {
								enddateSub = subtask.getDueDate();
								duedateSub = enddateSub.getTime();
								dueSub = new Timestamp(duedateSub);
							}

							try{

								if (cfm.getCustomFieldObjectByName("Start Date")==null) {

									timestampSub = subtask.getCreated();

								} else {

									 Object cfSub = subtask.getCustomFieldValue(cfm.getCustomFieldObjectByName("Start Date"));

									 if (cfSub==null) {

									 timestampSub = subtask.getCreated();

									 } else {

									 //System.out.println(cf);
									 SimpleDateFormat dateFormatSub = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
									 Date parsedDateSub = dateFormatSub.parse(cfSub.toString());
									 timestampSub = new java.sql.Timestamp(parsedDateSub.getTime());

									 }

								}


							 } catch(ParseException e) {
							 //this generic but you can control another types of exception look the origin of excption
								e.printStackTrace();
							 }

        	              	Timestamp createddateSub = timestampSub;

							if (subtask.isSubTask()) {

							objSub = new JSONObject()
							.put("Id", subtask.getId())
							.put("Name",subtask.getKey()+": "+subtask.getSummary())
							.put("PercentDone",percentDoneSub)
							.put("leaf",true)
							//.put("BaselineStartDate",createddateSub.toString())
							.put("StartDate",createddateSub.toString())
							.put("EndDate",dueSub.toString());
							//.put("parentId",subtask.getParentId())
							//.put("BaselineEndDate",dueSub.toString());
							children.put(objSub);

							} else {

						    }

						}

						subTasks.clear();

						//arr.put(obj);

						obj = new JSONObject()
							.put("Id", issueIter.getId())
							.put("Name",issueIter.getKey()+": "+issueIter.getSummary())
							.put("PercentDone",percentDone)
							.put("leaf",false)
							.put("expanded",true)
							//.put("BaselineStartDate",createddate.toString())
							.put("StartDate",createddate.toString())
							.put("EndDate",due.toString())
							//.put("BaselineEndDate",due.toString())
							.put("children",children);

						arr.put(obj);

						}

						//subTasks.clear();

					//}

					/*depencies = new JSONObject()
    				.put("Name","Created Date of: " + issueIter.getSummary())
    				.put("ParentId",issueIter.getId())
    				.put("IsLeaf","true")
                    .put("StartDate",createddate.toString())
                    .put("EndDate",createddate.toString());*/
        	    //}

				//arr.put(depencies);
                } catch (JSONException jerr) {
            	jerr.printStackTrace();
            	System.out.println("Got an JSONException: " + jerr.getCause());
                }

        	}

En laittanut koko koodia esille. Eli siis tuossa if-lauseessa on sellainen vika, että subTasks Collectioni menee kaikkiin JSON objekteihin vaikka sen pitäsi mennä vain siihen objektiin missä on subTaskeja.

Grez [22.11.2016 11:23:04]

#

Siis jos ajat tuota debugissa rivi kerrallaan, niin tämä rivi System.out.println(subTasks.size()); tulostaa nolla, mutta silti suoritus jatkuu tuonne else -lohkoon? :o

Ohimennen sanoen, eikö noita sisennyksiä voisi tehdä jotenkin järjellisesti? Esim. tähän tyyliin olisi helpompi lukea

while (iterator.hasNext())
{

    Issue issueIter = (Issue)iterator.next();

    Status status = issueIter.getStatusObject();
    //// koodia
    Collection<Issue> subTasks = issueIter.getSubTaskObjects();

    System.out.println(subTasks.size());

    if (subTasks.size() == 0)
    {

        obj = new JSONObject()
        .put("Id", issueIter.getId())
        .put("Name", issueIter.getKey() + ": " + issueIter.getSummary())
        .put("PercentDone", percentDone)
        .put("leaf", true)
        //.put("BaselineStartDate",createddate.toString())
        .put("StartDate", createddate.toString())
        .put("EndDate", due.toString());
        //.put("BaselineEndDate",due.toString());

        arr.put(obj);

    }
    else
    {

        for (Iterator subTasksIterator = subTasks.iterator(); subTasksIterator.hasNext();)
        {
            Issue subtask = (Issue)subTasksIterator.next();

            if (subtask.getOriginalEstimate() == null)
            {
                orgEstSub = 0.0;
                doubleOrgEstSub = Double.valueOf(orgEstSub);
                percentOrgEstSub = 0;
            }
            else
            {
                orgEstSub = Double.valueOf(subtask.getOriginalEstimate());
                doubleOrgEstSub = Double.valueOf(orgEstSub / 3600);
                percentOrgEstSub = subtask.getOriginalEstimate();
            }

            if (subtask.getEstimate() == null)
            {
                remEstSub = 0.0;
                doubleRemEstSub = Double.valueOf(remEstSub);
            }
            else
            {
                remEstSub = Double.valueOf(subtask.getEstimate());
                doubleRemEstSub = Double.valueOf(remEstSub / 3600);
            }

            if (subtask.getTimeSpent() == null || doubleOrgEstSub == 0.0)
            {
                gtsSub = 0;
                percentDoneSub = 0;
            }
            else
            {
                gtsSub = subtask.getTimeSpent();
                percentDoneSub = gtsSub * 100 / percentOrgEstSub;
            }

            if (subtask.getDueDate() == null)
            {
                enddateSub = subtask.getCreated();
                duedateSub = enddateSub.getTime() + 1 * 24 * 60 * 60 * 1000;
                dueSub = new Timestamp(duedateSub);
            }
            else
            {
                enddateSub = subtask.getDueDate();
                duedateSub = enddateSub.getTime();
                dueSub = new Timestamp(duedateSub);
            }

            try
            {

                if (cfm.getCustomFieldObjectByName("Start Date") == null)
                {

                    timestampSub = subtask.getCreated();

                }
                else
                {

                    Object cfSub = subtask.getCustomFieldValue(cfm.getCustomFieldObjectByName("Start Date"));

                    if (cfSub == null)
                    {

                        timestampSub = subtask.getCreated();

                    }
                    else
                    {

                        //System.out.println(cf);
                        SimpleDateFormat dateFormatSub = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
                        Date parsedDateSub = dateFormatSub.parse(cfSub.toString());
                        timestampSub = new java.sql.Timestamp(parsedDateSub.getTime());

                    }

                }


            }
            catch (ParseException e)
            {
                //this generic but you can control another types of exception look the origin of excption
                e.printStackTrace();
            }

            Timestamp createddateSub = timestampSub;

            if (subtask.isSubTask())
            {

                objSub = new JSONObject()
                .put("Id", subtask.getId())
                .put("Name", subtask.getKey() + ": " + subtask.getSummary())
                .put("PercentDone", percentDoneSub)
                .put("leaf", true)
                //.put("BaselineStartDate",createddateSub.toString())
                .put("StartDate", createddateSub.toString())
                .put("EndDate", dueSub.toString());
                //.put("parentId",subtask.getParentId())
                //.put("BaselineEndDate",dueSub.toString());
                children.put(objSub);

            }
            else
            {

            }

        }

        subTasks.clear();

        //arr.put(obj);

        obj = new JSONObject()
            .put("Id", issueIter.getId())
            .put("Name", issueIter.getKey() + ": " + issueIter.getSummary())
            .put("PercentDone", percentDone)
            .put("leaf", false)
            .put("expanded", true)
            //.put("BaselineStartDate",createddate.toString())
            .put("StartDate", createddate.toString())
            .put("EndDate", due.toString())
            //.put("BaselineEndDate",due.toString())
            .put("children", children);

        arr.put(obj);

    }

    //subTasks.clear();

    //}

    /*depencies = new JSONObject()
    .put("Name","Created Date of: " + issueIter.getSummary())
    .put("ParentId",issueIter.getId())
    .put("IsLeaf","true")
    .put("StartDate",createddate.toString())
    .put("EndDate",createddate.toString());*/
    //}

    //arr.put(depencies);

}

walkout_ [22.11.2016 11:34:44]

#

Grez kirjoitti:

Siis jos ajat tuota debugissa rivi kerrallaan, niin tämä rivi System.out.println(subTasks.size()); tulostaa nolla, mutta silti suoritus jatkuu tuonne else -lohkoon? :o

Juu koodi laittaa tuonne else-lohkoon subtaskeja vaikka ei pitäsi.

Lisäys:

Eli täälainen ulostulo pitäisi tulla poislukien se, että subtaskeilla ei voi olla subtaskeja.

{
    "success"      : true,
    "tasks" : {
        "rows" : [
            {
                "BaselineEndDate"   : "2010-02-01",
                "Id"                : 1,
                "Name"              : "Planning",
                "PercentDone"       : 50,
                "StartDate"         : "2010-01-18",
                "BaselineStartDate" : "2010-01-13",
                "Duration"          : 10,
                "expanded"          : true,
                "children"          : [
                    {
                        "BaselineEndDate"   : "2010-01-28",
                        "Id"                : 11,
                        "leaf"              : true,
                        "Name"              : "Investigate",
                        "PercentDone"       : 50,
                        "TaskType"          : "LowPrio",
                        "StartDate"         : "2010-01-18",
                        "BaselineStartDate" : "2010-01-20",
                        "Segments"          : [
                            {
                                "Id"                : 1,
                                "StartDate"         : "2010-01-18",
                                "Duration"          : 1
                            },
                            {
                                "Id"                : 2,
                                "StartDate"         : "2010-01-20",
                                "Duration"          : 2
                            },
                            {
                                "Id"                : 3,
                                "StartDate"         : "2010-01-25",
                                "Duration"          : 5
                            }
                        ]
                    },
                    {
                        "BaselineEndDate"   : "2010-02-01",
                        "Id"                : 12,
                        "leaf"              : true,
                        "Name"              : "Assign resources",
                        "PercentDone"       : 50,
                        "StartDate"         : "2010-01-18",
                        "BaselineStartDate" : "2010-01-25",
                        "Duration"          : 10
                    },
                    {
                        "BaselineEndDate"   : "2010-02-01",
                        "Id"                : 13,
                        "leaf"              : true,
                        "Name"              : "Gather documents (not resizable)",
                        "Resizable"         : false,
                        "PercentDone"       : 50,
                        "StartDate"         : "2010-01-18",
                        "BaselineStartDate" : "2010-01-25",
                        "Duration"          : 10
                    },
                    {
                        "BaselineEndDate"   : "2010-02-04",
                        "Id"                : 17,
                        "leaf"              : true,
                        "Name"              : "Report to management",
                        "PercentDone"       : 0,
                        "StartDate"         : "2010-01-30",
                        "BaselineStartDate" : "2010-01-29",
                        "Duration"          : 0
                    }
                ]
            },
            {
                "BaselineEndDate"   : "2010-03-15",
                "Id"                : 4,
                "TaskType"          : "LowPrio",
                "Name"              : "Implementation Phase 1",
                "PercentDone"       : 50,
                "StartDate"         : "2010-02-01",
                "BaselineStartDate" : "2010-01-23",
                "Duration"          : 10,
                "expanded"          : true,
                "children"          : [
                    {
                        "BaselineEndDate"   : "2010-01-25",
                        "Id"                : 34,
                        "leaf"              : true,
                        "Name"              : "Preparation work",
                        "PercentDone"       : 30,
                        "StartDate"         : "2010-02-01",
                        "BaselineStartDate" : "2010-01-20",
                        "Duration"          : 5
                    },
                    {
                        "BaselineEndDate"   : "2010-03-07",
                        "Id"                : 14,
                        "leaf"              : true,
                        "Name"              : "Evaluate chipsets",
                        "PercentDone"       : 30,
                        "BaselineStartDate" : "2010-02-25",
                        "StartDate"         : "2010-02-01",
                        "Duration"          : 5
                    },
                    {
                        "BaselineEndDate"   : "2010-03-20",
                        "Id"                : 16,
                        "leaf"              : true,
                        "Name"              : "Choose technology suite",
                        "PercentDone"       : 30,
                        "BaselineStartDate" : "2010-03-10",
                        "StartDate"         : "2010-02-01",
                        "Duration"          : 5,
                        "Rollup"            : true
                    },
                    {
                        "BaselineEndDate"   : "2010-03-06",
                        "Id"                : 15,
                        "Name"              : "Build prototype",
                        "PercentDone"       : 40,
                        "StartDate"         : "2010-02-08",
                        "BaselineStartDate" : "2010-01-28",
                        "Duration"          : 5,
                        "expanded"          : true,
                        "children"          : [
                            {
                                "BaselineEndDate"   : "2010-02-06",
                                "Id"                : 20,
                                "leaf"              : true,
                                "Name"              : "Step 1",
                                "PercentDone"       : 60,
                                "StartDate"         : "2010-02-08",
                                "BaselineStartDate" : "2010-01-27",
                                "Duration"          : 4
                            },
                            {
                                "BaselineEndDate"   : "2010-02-22",
                                "Id"                : 19,
                                "leaf"              : true,
                                "Name"              : "Step 2",
                                "PercentDone"       : 60,
                                "StartDate"         : "2010-02-08",
                                "BaselineStartDate" : "2010-02-17",
                                "Duration"          : 4
                            },
                            {
                                "BaselineEndDate"   : "2010-03-07",
                                "Id"                : 18,
                                "leaf"              : true,
                                "Name"              : "Step 3",
                                "PercentDone"       : 60,
                                "StartDate"         : "2010-02-08",
                                "BaselineStartDate" : "2010-02-25",
                                "Duration"          : 4
                            },
                            {
                                "BaselineEndDate"   : "2010-03-08",
                                "Id"                : 21,
                                "leaf"              : true,
                                "Name"              : "Follow up with customer",
                                "PercentDone"       : 60,
                                "StartDate"         : "2010-02-12",
                                "BaselineStartDate" : "2010-03-04",
                                "Duration"          : 1,
                                "Rollup"            : true
                            }
                        ]
                    }
                ]
            },
            {
                "BaselineEndDate"   : "2010-03-08",
                "Id"                : 5,
                "leaf"              : true,
                "Name"              : "Customer approval",
                "PercentDone"       : 0,
                "BaselineStartDate" : "2010-03-01",
                "StartDate"         : "2010-02-13",
                "Duration"          : 0,
                "TaskType"          : "CustomerApproval",
                "Rollup"            : true
            },
            {
                "BaselineEndDate"   : "2010-03-18",
                "Id"                : 6,
                "Name"              : "Implementation Phase 2",
                "PercentDone"       : 50,
                "BaselineStartDate" : "2010-03-08",
                "StartDate"         : "2010-02-15",
                "Duration"          : 8,
                "expanded"          : true,
                "TaskType"          : "Important",
                "children"          : [
                    {
                        "BaselineEndDate"   : "2010-03-18",
                        "Id"                : 25,
                        "leaf"              : true,
                        "Name"              : "Task 1",
                        "PercentDone"       : 10,
                        "BaselineStartDate" : "2010-03-08",
                        "StartDate"         : "2010-02-15",
                        "Duration"          : 8
                    },
                    {
                        "BaselineEndDate"   : "2010-03-18",
                        "Id"                : 26,
                        "leaf"              : true,
                        "Name"              : "Task 2",
                        "PercentDone"       : 20,
                        "BaselineStartDate" : "2010-03-08",
                        "StartDate"         : "2010-02-15",
                        "Duration"          : 8
                    },
                    {
                        "BaselineEndDate"   : "2010-03-18",
                        "Id"                : 27,
                        "leaf"              : true,
                        "Name"              : "Task 3",
                        "PercentDone"       : 20,
                        "StartDate"         : "2010-02-15",
                        "BaselineStartDate" : "2010-03-08",
                        "Duration"          : 8,
                        "Rollup"            : true
                    }
                ]
            },
            {
                "BaselineEndDate"   : "2010-03-17",
                "Id"                : 10,
                "leaf"              : true,
                "Name"              : "Customer approval 2 (not draggable)",
                "Draggable"         : false,
                "PercentDone"       : 0,
                "StartDate"         : "2010-02-25",
                "BaselineStartDate" : "2010-03-17",
                "Duration"          : 0
            },
            {
                "BaselineEndDate"   : "2010-05-08",
                "Id"                : 8,
                "Name"              : "Production phase 1",
                "PercentDone"       : 40,
                "StartDate"         : "2010-02-25",
                "BaselineStartDate" : "2010-02-22",
                "Duration"          : 35,
                "expanded"          : true,
                "children"          : [
                    {
                        "BaselineEndDate"   : "2010-04-07",
                        "Id"                : 22,
                        "leaf"              : true,
                        "Name"              : "Assemble",
                        "PercentDone"       : 50,
                        "StartDate"         : "2010-02-25",
                        "BaselineStartDate" : "2010-03-22",
                        "Duration"          : 12
                    },
                    {
                        "BaselineEndDate"   : "2010-04-21",
                        "Id"                : 23,
                        "leaf"              : true,
                        "Name"              : "Load SW",
                        "PercentDone"       : 20,
                        "StartDate"         : "2010-03-15",
                        "BaselineStartDate" : "2010-04-06",
                        "Duration"          : 11
                    },
                    {
                        "BaselineEndDate"   : "2010-05-04",
                        "Id"                : 24,
                        "leaf"              : true,
                        "Name"              : "Basic testing (inc some test)",
                        "PercentDone"       : 50,
                        "BaselineStartDate" : "2010-04-14",
                        "StartDate"         : "2010-03-30",
                        "Duration"          : 12
                    }
                ]
            },
            {
                "BaselineEndDate"   : "2010-05-11",
                "Id"                : 9,
                "leaf"              : true,
                "Name"              : "Final testing",
                "PercentDone"       : 0,
                "BaselineStartDate" : "2010-05-02",
                "StartDate"         : "2010-04-15",
                "Duration"          : 6
            },
            {
                "BaselineEndDate"   : "2010-05-11",
                "Id"                : 7,
                "leaf"              : true,
                "Name"              : "Delivery",
                "PercentDone"       : 40,
                "BaselineStartDate" : "2010-05-11",
                "StartDate"         : "2010-04-23",
                "Duration"          : 0
            }
        ]
    }
}

Metabolix [22.11.2016 17:16:48]

#

Ongelma on varmaan se, että et tyhjennä children-muuttujaa kierrosten välillä, eli jokaiselle kohdalle tulee children-muuttujasta omat ja kaikkien edellisten kohtien subtaskit. Eli olennaista olisi siirtää children-muuttuja kokonaan while-silmukan sisään, jotta se luodaan joka kierroksella uudestaan. (Sen sijaan subTasks.clear() on luultavasti väärin, ellei sillä ole jotain merkitystä muualla ohjelmassasi.)

walkout_ [22.11.2016 17:51:57]

#

Metabolix kirjoitti:

(22.11.2016 17:16:48): Ongelma on varmaan se, että et tyhjennä...

Moi, millä komennolla tyhjennän children-muutujan?

Metabolix [22.11.2016 17:59:12]

#

Siirrä koko muuttuja while-silmukkaan, niin ei tarvitse erikseen tyhjentää sitä. Esimerkistäsi olettaisin, että muuttuja on JSONArray, eli näin:

while (iterator.hasNext()) {
   JSONArray children = new JSONArray();
   // ...
}

P.S. Etkö olekaan vielä rautainen ammattilainen?

walkout_ [22.11.2016 18:10:57]

#

Metabolix kirjoitti:

P.S. Etkö olekaan vielä rautainen ammattilainen?

Java-taidoissa on vielä puutetta, mutta PHP/MySQL/AJAX-ohjelmointiin en tarvitse apua.

Lisäys: Kiitos avusta. Koodi toimii nyt kuten pitää!

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta