3

I have a program that retrieves value in an sqlite table using ArrayList. I am retrieving string data from it and count the length of string. The string will then be concatenated to another data if the total string count is less than or equal to 160. if total string count is greater than 160, it will not add the string.

The concept is like this:

  1. retrieve string
  2. variable a(which will hold the strings) will have the string retrieved concatenated to it.
  3. checks if a's length is less than or equal to 160. if less than go back to step (1), if not stop concatenating.

Updated

  1. If stop concatenating, the string will be stored.
  2. Process continues for the remaining string in the pending list.

However, my program just add all strings without even counting if the length is over 160. This is my code:

List<Pending> pending = db.getAllPending();
 String a="";
  do{
      for (Pending pn : pending) {
          a += pn.getPm_str();
       }
  }while(a.length()<=160);
  Log.d("s", a); 

Logcat:

RBSN/C1/12/3/4/8,CVTE/C1/2/3/4/1,RBSN/C1/4/2131/331/12,RBSN/C1/45/67/12/44,RBSN/C1/44/231/123/23213,RBSN/C1/444/55522/222/11,RBSN/C1/123/123/213123/1231232,RBSN/C1/2/3/1/1,RBSN/C1/123/123/213123/1231232,

(string length is 172.)

Expected output should be

RBSN/C1/12/3/4/8,CVTE/C1/2/3/4/1,RBSN/C1/4/2131/331/12,RBSN/C1/45/67/12/44,RBSN/C1/44/231/123/23213,RBSN/C1/444/55522/222/11,RBSN/C1/123/123/213123/1231232,RBSN/C1/2/3/1/1,

(string length is 156.)

Follow-up Question Also, what should i do if i want to repeat the same process again but this time, the process will affect the strings which are not included in the first 160?

user5083862
  • 67
  • 1
  • 6

5 Answers5

4

In Java, never concatenate Strings inside a loop, instead of this, use StringBuilder and check after each iteration if necessary to add more strings:

List<Pending> pending = db.getAllPending();
StringBuilder a = new StringBuilder();

for (Pending pn : pending) {
    a.append(pn.getPm_str());
    if (a.length > 160) break;
}
Log.d("s", a.toString()); 
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • your answer is incorrect..you append the string & check the length..for (ex) if appended string is 172 then only loop break :) – Ranjithkumar Aug 19 '15 at 10:37
  • @ranjith this is what OP asks... *checks if a's length is less than or equal to 160. if less than go back to step (1), if not stop concatenating.* what you get here???? if length is less than 160 append more, if not break... please read carefully what OP asks and not what you gess before. – Jordi Castilla Aug 19 '15 at 10:41
  • He want output not greater than 160..see the logcat & expected output in this qtn.. – Ranjithkumar Aug 19 '15 at 10:48
  • 1
    and you answered wrong also... but nice for what OP was looking for – Jordi Castilla Aug 19 '15 at 11:00
2

Check this answer,

for (Pending pn : pending) {
   if (a.length() + pn.getPm_str().length() <= 160) {
    a += pn.getPm_str();
}
else
break;
}

Update

ArrayList<String> nameGroups=new ArrayList<>();

for (Pending pn : pending) {
   if (a.length() + pn.getPm_str().length() <= 160) {
    a += pn.getPm_str();
   }
  else{
   nameGroups.add(a);    
   a="";
   a += pn.getPm_str();
   }
}
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • The code works for me. what if i want to do the same process again but this time it will loop for the remaining datas in the table? Say the first 160 are grouped and the remaining will be grouped also? – user5083862 Aug 19 '15 at 10:36
  • r u want list of groups? – Ranjithkumar Aug 19 '15 at 10:44
  • sort of. the datas in the table will be concatenated to create a 160-length string. every group of datas will have 160 length or less depending on the available pending datas. – user5083862 Aug 19 '15 at 10:47
1

Try this

 List<Pending> pending = db.getAllPending();
 String a="";
 for (Pending pn : pending) {
      if(a.length()<=160){
        a += pn.getPm_str();
       }else
       {
              break;
       }
   }
  Log.d("s", a); 
Iamat8
  • 3,888
  • 9
  • 25
  • 35
N J
  • 27,217
  • 13
  • 76
  • 96
1

The problem originates I believe is in your for loop. It goes through all elements in "pending" and THEN checks the while condition. If you drop the while loop and just replace your for loop with this it should work:

for (Pending pn : pending) {
    if(a.length()>160)
         break;
    a += pn.getPm_str();
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Templerschaf
  • 142
  • 1
  • 8
1

If you want to make sure your string is never above 160 characters in length, you can also do this:

List<Pending> pending = db.getAllPending();
StringBuilder a = new StringBuilder();

for (Pending pn : pending) { 
    String toAdd = pn.getPm_str();
    if (a.length() + toAdd.length() <= 160) {
      a.append(toAdd);
    } else {
      break;
    }
}
Vlad
  • 18,195
  • 4
  • 41
  • 71