Home > DeveloperSection > Forums > How to Print Multiple Statements to One TextView
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    May-30-2013 12:09 AM

 Android Android 
Ratings:


 1 Reply(s)
 691  View(s)
Rate this:
Hi Everyone!

I have the following code:

for(int i = 1; i <= doubleLength; i++) {
    doubleRate = (doubleBalance * (doubleRate/100))/12;
    doubleBalance = doubleBalance + doubleRate;
    doublePayment = (doubleBalance/doubleCount);

    TextView results = (TextView) findViewById(R.id.showResult);
    results.setText(doublePayment+"");

    doubleCount = doubleCount - 1;
    doubleBalance -= doublePayment;
}

What I'm trying to do is print out every single value of the "doublePayment" value to a TextView UI Object on the screen. However, when the for loop finishes, it only 

prints out one value, instead of several.

I'm porting this over from C++, so I'm used to simply using count to print to the terminal.

Thanks in advance!



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-30-2013 5:45 AM

Hi Jayden!

Try this, maybe you made a mistake to Print Multiple Statements to One TextView

results.setText(doublePayment+"");
You're re setting the text value instead of appending the whole String. A better approach would be using a StringBuilder:

StringBuilder sb = new StringBuilder();
for(int i = 1; i <= doubleLength; i++) {
    doubleRate = (doubleBalance * (doubleRate/100))/12;
    doubleBalance = doubleBalance + doubleRate;
    doublePayment = (doubleBalance/doubleCount);

    sb.append(doublePayment);
    sb.append(" ");

    doubleCount = doubleCount - 1;
    doubleBalance -= doublePayment;
}
TextView results = (TextView) findViewById(R.id.showResult);
results.setText(sb.toString());

I hope this code might be resolve your problem. 

Don't want to miss updates? Please click the below button!

Follow MindStick