HOW TO PRINT MULTIPLE STATEMENTS TO ONE TEXTVIEW

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
 1010  View(s)
Ratings:
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!

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to Print Multiple Statements to One TextView

    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. 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!