1

I need help to sum all the integers in the arrayList and view the result of the sum in the text view, all the eight number is from edit text here is the full code that get the integer from edit text and view the sum result on the text view

 public class MainActivity extends AppCompatActivity {

EditText editText;
Button button;
TextView textView;

int one, two, three, four, five, six, seven, eight;

int Result;
private static final String TAG = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    textView = (TextView) findViewById(R.id.text);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            String data = editText.getText().toString();

            one = Integer.parseInt(data.charAt(0) + "");
            two = Integer.parseInt(data.charAt(1) + "");
            three = Integer.parseInt(data.charAt(2) + "");
            four = Integer.parseInt(data.charAt(3) + "");
            five = Integer.parseInt(data.charAt(4) + "");
            six = Integer.parseInt(data.charAt(5) + "");
            seven = Integer.parseInt(data.charAt(6) + "");
            eight = Integer.parseInt(data.charAt(7) + "");


            int[] mNumber = {one, two * 2, three, four * 2, five, six * 2, seven, eight * 2};
            for (int n : mNumber) {
                spiltInt(n);
            }
        }

    });
}

public void spiltInt(int x) {
    String number = String.valueOf(x);
    ArrayList<Integer> list = new ArrayList<>();
    for (int i = 0; i < number.length(); i++) {
        int j = Character.digit(number.charAt(i), 10);
        list.add(j);
    }



}

}

3
  • 1
    It's not quite clear what you want to achieve, can you exemplify ? Commented Sep 16, 2017 at 7:34
  • i edit the question @SchiduLuca Commented Sep 16, 2017 at 7:43
  • is there any use of Splitting the values? Commented Sep 16, 2017 at 7:50

3 Answers 3

3

Assuming you want to sum all the digits from an array of integers, here's a 1-liner:

int digitSum = IntStream.of(numbers).flatMap(n -> ("" + n).chars()).map(i -> i - '0').sum();
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this

    //Assumig variables one,two,three,four,five,six are defined above as int
    int[] mNumber = {one, two * 2, three, four * 2, five, six * 2, seven, eight * 2};
        for (int n : mNumber) {
            spiltInt(n);
        }
       showSum(mNumber);

public void spiltInt(int x) {
    String number = String.valueOf(x);
    ArrayList<Integer> list = new ArrayList<>();
    for (int i = 0; i < number.length(); i++) {
      int j = Character.digit(number.charAt(i), 10);
      list.add(j);
     }
     int result = 0;
     for(Integer i:list){
        result+=i;
     }

      textView.setText(result+"");
  }

  public static void showSum(int[] array){
   int sum = 0;
   int digitSum = 0;
   for(int i=0;i<array.length;i++){
       sum+=array[i];

       String number = String.valueOf(array[i]);
       ArrayList<Integer> list = new ArrayList<>();
      for (int j = 0; j < number.length(); j++) {
         int k = Character.digit(number.charAt(j), 10);
         list.add(k);
      }
      for(Integer digit:list){
        digitSum+=digit;
     }
   }
   //you can set any of these into test view using setText method
   System.out.println(sum+"");
   System.out.println(digitSum+"");
 }

Comments

0

You are calling spiltInt() which is a non static method and it cannot be referenced from a static context.

Just make you spiltInt() as static and call it using the class name which is MainActivity in this case.

your code becomes

for (int n : mNumber) {
      MainActivity.spiltInt(n);

        }
 textView.setText(result+"");

Now, to print the result make result variable global.

and use the following code at the end of your spiltInt() code

like

for(int k : list){
 result = result+k;
}

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.