0

I am trying to store my EditTexts in an array and display all of them in a TextView, then have a sum function to get the sum of the array and display the sum. But I am stuck at this point.

public void Clickme (View view) {
   public void clickMe (View v){
    //my EditText’s ID is called myInput
        EditText myInput = (EditText) findViewById(R.id.myInput);
    //my TextView’s ID is called text2
        TextView text2 = (TextView) findViewById(R.id.text2);
        String str = myInput.getText().toString();
        text2.setText(str);
        int [] array = new int [myInput.length()];
        //I am stuck at this point
        for(int i=0; i <myInput.length(); i++) {
            array[i] = Integer.valueOf(myInput.getText(i));
        }
       //sum function
       for (int i: array) {
        str = str + i;
        EditText.setText(str);
    }
}
0

1 Answer 1

2

You have a method in a method... That won't compile

public void Clickme (View view) {
   public void clickMe (View v){

And what are you trying to do here? EditText.setText(str);

setText is not a static method.

then have a sum function to get the sum of the array and display the sum

And here? str = str + i;

That isn't a sum of numbers. You are appending a number to a string.

And this method doesn't exist... myInput.getText(i)


Based on your loop and array creation, looks like you want to add all the digits of a number.

private EditText myInput;
private TextView text2;

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(...);
    ...

    myInput = (EditText) findViewById(R.id.myInput);
    text2 = (TextView) findViewById(R.id.text2);
} 

public void clickMe (View view) {
    String input = myInput.getText().toString();
    int number = 0;
    if (!input.isEmpty()) {
        number = Integer.parseInt(input);
    }
    // Sum the digits in a number
    int sum = 0;
    while (number > 0) {
        sum += number % 10;
        number = number / 10;
    }
    text2.setText(String.valueOf(sum));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. This is really helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.