Index in for method is not increasing Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Index in for method is not increasing without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Ok this might seem easy but its been bugging my mind for days and I honestly don’t know why it the index wont increase and get the other data. I dont know where to but the return. I placed it in 2 lines and the first one only gives the first row of data from the database and the second one only gives the last one from the database. (See commented out lines below). How to get each row that fits the if-statements?

Here’s my code:

public Object[] populateTable(ArrayList<Outlet> outletList, String selection, int size, int monthCtr, String selDay){ 

    for(int i = 0; i<outletList.size(); i++){
        if(outletList.get(i).getCity().equalsIgnoreCase(selection)){
            if(outletList.get(i).getStatus().equals("ACTIVE")){
                bar = outletList.get(i).getBarangay();
                code = Integer.toString(outletList.get(i).getCode());
                name = outletList.get(i).getName();

                data = new Object[]{bar, name, code};
                //return data ->gives the first one in the database
            }
        }
    }
  }
   //return data -> gives the last one in the database
}

Answer

You need to save all your results in another array and return that instead.

public Object[] populateTable(ArrayList<Outlet> outletList, String selection, int size, int monthCtr, String selDay)
{ 
    List<object> result = new List<object>();

    for(int i = 0; i<outletList.size(); i++)
    {
        if(outletList.get(i).getCity().equalsIgnoreCase(selection))
        {
            if(outletList.get(i).getStatus().equals("ACTIVE"))
            {
                bar = outletList.get(i).getBarangay();
                code = Integer.toString(outletList.get(i).getCode());
                name = outletList.get(i).getName();

                data = new Object[]{bar, name, code};
                result.Add(data);
            }
        }
    }
    return result.ToArray();
}
We are here to answer your question about Index in for method is not increasing - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji