Search code examples
javaandroidmultiplication

multiplication table in android using java


Implementing a simple multiplication table in android. this code doesn't help in displaying the result. where am I going wrong?

@Override
protected void onCreate(Bundle savedInstanceState) {
    final int a;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // final TextView finalet = null;
    final TextView finale = (TextView) findViewById(R.id.finale);
    final EditText et = (EditText) findViewById(R.id.et);
    final String fromEt = et.getText().toString();

    Button gen = (Button) findViewById(R.id.gen);
    gen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                int a = Integer.parseInt(fromEt);
                for (int voo = 0; voo <= 11; voo++) {
                    Integer sam = a * voo;
                    // finalet.setText(a*v);
                    // finale.setText(String.valueOf(sam));
                    finale.setText(sam);
                }
            } catch (NumberFormatException e) {

            }
        }
    });
}

Solution

  • Change this part:

    int a = Integer.parseInt(fromEt); 
    

    to

    int a = Integer.parseInt(et.getText().toString());
    

    like this:

    public void onClick(View v) {
                try {
    
                    int a = Integer.parseInt(et.getText().toString());
                    for (int voo = 0; voo <= 11; voo++) {
                        Integer sam = a * voo;
                        finale.setText(String.valueOf(sam));
                    }
                } catch (NumberFormatException e) {
    
                }
            }
    

    But your according to your code you will see only the last i.e the number you entered * 11

    If you want to show all you should do something like this:

    finale.append(String.valueOf(sam) + " ");
    

    inside the for loop