1
package com.culligandev.www;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class threeRows extends Activity {

    TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.output);
        display = (TextView) findViewById(R.id.tvDisplay);

        int a = 3;

        for(int i = 0; i < a; i++) {
            for(int j = 0; j <= i; j++) {
                display.setText("*");
            }
            display.setText("");
        }
    }
}

This activity is called by the press of a button and then should output a triangle made of asterisks like this:

*
**
***

It doesn't output anything and just shows a blank screen. Can you help, please?

1
  • it won't make a triangle, because every time you call setText it sets a new text, so the last sequence to be entered to the textview will be "", which means after you run this code, your TextView will contain "" in it Commented Dec 5, 2011 at 0:14

4 Answers 4

1

setText is not working as printf or println

if you want to display a pattern then you can do it using StringBuilder or StringBuffer and then use that string buffer/builder object in setText to display the pattern

Sign up to request clarification or add additional context in comments.

2 Comments

what would a stringBuilder look like?
java2s.com/Tutorial/CSharp/0100__String/0400__StringBuilder.htm hear is the example of string builder that you might need to check
1

You have problem in your "for". The last thing your app will do is display.setText(""), which means your text will be empty. You should put something like

display.setText(display.getText() + "\n");

And for the other one:

display.setText(display.getText() + "*");

Comments

0

TextView.append might be what you're after, or use a StringBuilder as has been suggested

Comments

0

try this:

for(int i = 0; i < a; i++) {
      for(int j = 0; j <= i; j++) 
      {
                display.setText(display.getText().toString().trim()+"*");
      }
      display.setText(display.getText().toString().trim()+"\n");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.