-4

I would like to get StringBuilder's value in a String variable:

        String strName = detailArrayListFromDB.get(i).getName();
        int dbQty = detailArrayListFromDB.get(i).getQuantity();
        double dbTotal = detailArrayListFromDB.get(i).getTotal();

        Log.d("Name::--", strName);
        Log.d("QTY::--", ""+dbQty);
        Log.d("TOTAL::--", ""+dbTotal);

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("Name: "+strName);
        strBuilder.append("Qty:" +dbQty);
        strBuilder.append("Total:" +dbTotal);
        String str = strBuilder.toString();
        Log.d("stringBuilder:", str);

And Log shows:

D/Name::--(20881): Mobile
D/QTY::--(20881): 1
D/TOTAL::--(20881): 20000.0
D/stringBuilder:(20881): Name: MobileQty:1Total:20000.0

D/Name::--(20881): Laptop
D/QTY::--(20881): 1
D/TOTAL::--(20881): 30000.0
D/stringBuilder:(20881): Name: LaptopQty:1Total:30000.0

This is what, I would like to get output:

D/stringBuilder:(20881): Name: Mobile Qty:1 Total:20000.0, Name: Laptop Qty:1 Total:30000.0
Oreo
  • 2,586
  • 8
  • 38
  • 63
  • I do not get where the problem is - you actually get exactly what you are expecting - you just have to append spaces between data components – Chaosit Aug 19 '15 at 09:46
  • don't use the String's concatenation. Call append more times – Blackbelt Aug 19 '15 at 09:46
  • If you want to have all Items in one String, use the `strBuilder`again in a loop with no new initialization of it. – MrT Aug 19 '15 at 09:47
  • @MrT yes how can i do this ? – Oreo Aug 19 '15 at 09:48
  • what's wrong here ? why this has been downvoted ... – Oreo Aug 19 '15 at 09:49
  • @Oreo, you already have a loop, initialize the StringBuilder outside of it and call `toString()` after the loop. – Chaosit Aug 19 '15 at 09:49
  • @Oreo as for downvotes - it's simple: here high-quality and well researched questions are appreciated. This one is neither. – Chaosit Aug 19 '15 at 09:51
  • You might have deeper problems, if you're using 6 lines of code to log something out... Firstly, it is not actually faster to use stringbuilder than just string concatenation (see http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java) . Secondly, I 'd suggest to look at a logging framework, google slf4j – drrob Aug 19 '15 at 09:54
  • I think this is quite simple. You just need to create a StringBuilder and use a loop to append all info you would like to log to your builder. Then you will have the result you want. – Kenny Tai Huynh Aug 19 '15 at 09:54

2 Answers2

2

To enter a Loop do for example this:

       StringBuilder strBuilder = new StringBuilder();
      for(int i = 0; i < <waht u need>; i++){
            String strName = detailArrayListFromDB.get(i).getName();
            int dbQty = detailArrayListFromDB.get(i).getQuantity();
            double dbTotal = detailArrayListFromDB.get(i).getTotal();

            Log.d("Name::--", strName);
            Log.d("QTY::--", ""+dbQty);
            Log.d("TOTAL::--", ""+dbTotal);


            strBuilder.append(" Name: "+strName);
            strBuilder.append(" Qty:" +dbQty);
            strBuilder.append(" Total:" +dbTotal);
    }
    String str = strBuilder.toString();
    Log.d("stringBuilder:", str);
MrT
  • 594
  • 2
  • 17
  • I ticked your answer as useful, will accept in 3 minutes – Oreo Aug 19 '15 at 09:56
  • Except, I'm assuming that to log into a single line, you need to lose the three Log.d calls i.e. Log.d("Name::--") etc. – drrob Aug 19 '15 at 10:02
1

A StringBuilder is just a way to concat strings together. It doesnt add formatting you have to do that yourself. For example:

strBuilder.append("Name: "+strName + " ");
strBuilder.append("Qty:" +dbQty + " ");
strBuilder.append("Total:" +dbTotal + ", ");
MidasLefko
  • 4,499
  • 1
  • 31
  • 44