1

I am attempting to create a calculator app, when I try to add 2 decimals together, I do not get the results I anticipated.

examples:

user inputs [1.1 + 1.1] and [1.1 + 1] or 2.1 is returned.

It is as if as if second value is being truncated, I am not entirely sure where this would be occurring

user inputs[2.1 + 2] and [2.1 + 2] or 4.1 is returned

user inputs [2 + 2.1] and [2 + 2] or 4 is returned

I have not used the debugger, though I have placed print line statements through out the code and I have not been able to see the issue.

Code:

information

  • I only attached a single number (image button) as all numbers 1-9 have identical code
  • variables that are not instantiated in code below have been instantiated as "" if they are a string and 0.0 if they are a double
  • I am using an array list called values which takes in strings, and using double.parsedouble() to convert it to a double, this occurs in the equals method
    • please let me know if their is anything I need to add to make this question easier to understand, and any help is appreciated

thank you!

1 button

one_button_ET.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (plus_selected) {
                update_selected_buttons(); //for boolean variables and to change mathematical function button image
                screen_value = "";
                updateScreen("1");
                System.out.println(screen_value);
                values.add(screen_value);

            } else {
                updateScreen("1");
                System.out.println(screen_value);

            }

        }


    });

addition button

addition_button_ET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!plus_selected) {
                    plus_selected = true;
                    addition_button_ET.setImageResource(R.drawable.addition_selected_500_500);
                    if(Double.parseDouble(screen_value) == current_value){

                    }
                    else
                    values.add(screen_value);//
                } else {
                    plus_selected = false;
                    addition_button_ET.setImageResource(R.drawable.addition_button_500_500);
                }


            }
        });

update screen

private void updateScreen(String value) { //being used for only one_button

        //to reset title text
        if (screen_value.equals(initial_screen_value)) {
            screen_value = "0.0";
            screen_TV.setTextSize(50);
        }
        //clears default value (0.0), allowing user to enter in a number
        if (screen_value.equals("0.0")) {
            screen_value = value;
            screen_TV.setText(screen_value);
        } else {
            screen_value += value;
            System.out.println(screen_value);
            screen_TV.setText(screen_value);
        }

    }

clear button (clear button calls this method, and that is the only parameter it has)

private void resetScreen() {
        screen_value = "0.0";
        //values.clear();
        screen_TV.setTextSize(50);
        screen_TV.setText(screen_value);

    }

equals button

equals_button_ET.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                current_value = 0;
                sum = 0;
                System.out.println(values);
                for(int i = 0; i<values.size();i++) {
                    sum += Double.parseDouble(values.get(i));
                }

                current_value = sum;
                screen_value = "" + sum;
               screen_TV.setText(screen_value);

            }
        });
Lontronix
  • 193
  • 2
  • 15
  • I think this might be a duplicate of this question, and you may need parseFloat. https://stackoverflow.com/questions/15748099/double-parsedouble-is-not-working-on-my-webpage – Katie May 23 '17 at 20:20
  • @katie that is java script not java – Lontronix May 25 '17 at 16:55

0 Answers0