-1

I am trying to get a sum from all the user numbers input. User enter a quantity number from dialog. The quantity is the shown on a TextView, inside the ArrayList quantity. In the ArrayList quantity, i can get position(0) and position(1). But the idea is to ge all the Arraylist positions and esecute the sum of the numbers.

I am trying with this code, but obviously something is not right.

int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));

This is the Activity

public class CalculatorActivity extends AppCompatActivity {

Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity;
Dialog dialog;
RecyclerView recyclerView;
ArrayList<String> text = new ArrayList<>();
ArrayList<String> quantity = new ArrayList<>();


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

    nameTV = findViewById(R.id.nameTV);
    quantityTV = findViewById(R.id.qunatityTV);
    resulttry = findViewById(R.id.resulttry);
    btnCclcola = findViewById(R.id.btn_calcola);

    recyclerView = findViewById(R.id.rv_calculator);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    CalculatorAdapter calculatorAdapter = new CalculatorAdapter(this, text, quantity);
    recyclerView.setAdapter(calculatorAdapter);

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

            int total1 = Integer.parseInt(quantity.get(0));
            int total2 = Integer.parseInt(quantity.get(1));
            int total = 0;
            for (int i = 0; i < quantity.size(); i++) {
                total = total2 + total1 ;
            }
            System.out.println(Integer.valueOf(total));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
   

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {...}
    

private void opendialog() {...}


private void validateCategory() {...}
   

private void saveInfo() {
    String ingName = ingrName.getText().toString();
    int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
    
    text.add(ingName);
    quantity.add(String.valueOf(Integer.valueOf(ingQuant)));

}
Giovanni
  • 512
  • 2
  • 6
  • 23
  • why `Integer.valueOf(total)` ? – Scary Wombat May 20 '22 at 08:49
  • 1
    It looks like your main problem is that you don't know how to iterate over your List, maybe [Ways to iterate over a list in Java](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) will help you. – OH GOD SPIDERS May 20 '22 at 08:49

2 Answers2

0

Your loop computes total2 + total1 for every entry of quantity but you don't use the iteration values.

The Stream-API will help you a lot. Create a Stream over quantity, parse them to int and summarize

int sum = quantity.stream()
    .mapToInt(Integer::parseInt)
    .sum();
Stefan Warminski
  • 1,845
  • 1
  • 9
  • 18
0

It's fine. I find it by my self. The correct code is:

            int total = 0;
            
            for (int i = 0; i < quantity.size(); i++) {
                int total2 = Integer.parseInt(quantity.get(i));
                total += total2;
                
                Toast.makeText(CalculatorActivity.this, ""+ total, Toast.LENGTH_SHORT).show();
Giovanni
  • 512
  • 2
  • 6
  • 23