-1

I'm new to Android. I'm trying to develop my first calculator. My calculator output is good, but I'm trying to make some changes to it. Please suggest. My output is 2+2=4.0 How can I get 4 if I put 2+2 and 4.0 when I put 2.8+1.2.

Also, please help me out in trying to figure out how can i keep on adding till i press =.

My code that I'm looking at is below:

   private View.OnClickListener buttonClickListerner = new
   View.OnClickListener() {
    float r;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear:
                screen.setText("");
                operator.setText("");
                FirstNum= 0;
                showtext.setText("");
                break;
            case R.id.buttonAdd:
                mMath("+");
                operator.setText("+");
                showtext.setText(String.valueOf(FirstNum));
                break;
            case R.id.buttonMinus:
                mMath("-");
                operator.setText("-");
                break;
            case R.id.buttonMul:
                mMath("*");
                operator.setText("*");
                break;
            case R.id.buttonequal:
                mResult();
                break;
            case R.id.buttonDiv:
                mMath("/");
                operator.setText("/");
                break;
            case R.id.buttonPercent:
                mMath("%");
                r =  FirstNum / 100;
                showtext.setText("[" + String.valueOf(FirstNum) + "%" + "]");    
                screen.setText(String.valueOf(r));
                break;
            default:
                String num = ((Button) v).getText().toString();
                getKeyboard(num);
                break;
        }
    }
    };

    public void mMath(String str){
    FirstNum = Float.parseFloat(screen.getText().toString());
    operation = str;
    screen.setText("");
    }

    public void getKeyboard(String str){
    String CurrentScreen = screen.getText().toString();
    if(CurrentScreen.equals("0"))
        CurrentScreen = "";
    CurrentScreen = CurrentScreen + str;
    screen.setText(CurrentScreen);
    String ExScreen = CurrentScreen;
    screen.setText(ExScreen);
    }

    public void mResult(){
    float SecondNum = Float.parseFloat(screen.getText().toString());
    float ThirdNum = Float.parseFloat(screen.getText().toString());
    float result = 0;
    //float exresult = result;

    if(operation.equals("+")){
        result = FirstNum + SecondNum;
       // exresult = result + ThirdNum;
    }
    if(operation.equals("-")){
        result = FirstNum - SecondNum;
        //exresult = result - ThirdNum;
    }
    if(operation.equals("*")){
        result = FirstNum * SecondNum;
        //exresult = result * ThirdNum;
    }
    if(operation.equals("/")){
        result = FirstNum / SecondNum;
        //exresult = result / ThirdNum;
    }
    screen.setText(String.valueOf(result));
    //screen.setText(String.valueOf(exresult));
    showtext.setText(String.valueOf(FirstNum + operation + SecondNum));
    //showtext.setText(String.valueOf(FirstNum + operation + SecondNum +           
    operation + ThirdNum));
   }
   }
Biffen
  • 6,249
  • 6
  • 28
  • 36
ferrari
  • 1
  • 1

2 Answers2

0

I guess you should do your calculations as double and then before setting the output to TextView (or whatever you are using), check for the output if int or not and then decide which form of output to set to the TextView.

if ((variable == Math.floor(variable)) && !Double.isInfinite(variable)) {
    // integral type
}

See this

Edit:

The idea is to check that fractional part of the number is 0 (i.e.) the number is integer.

You may also Use these conditions [if true then variable is an Integer]

// check if
variable == Math.ceil(variable)  

or

// check if
variable == Math.round(variable)

Also Math.round(float f) will return the interger form of the number!

Community
  • 1
  • 1
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
-1

To add multiple item first set up an array with a size of how long the user can input and then loop through each array adding them equivalently... i know this is a vague answer but you can ask me if anything is unclear and also an up vote would be nice. you got the right idea for the cases just try the following code

 // array to sum

    int[] numbers = new int[]{ 10, 10, 10, 10};



    int sum = 0;



    for (int i=0; i < numbers.length ; i++) {

        sum = sum + numbers[i];

    }



    System.out.println("Sum value of array elements is : " + sum);



}
dag mawi
  • 1
  • 2
  • Welcome to Stack Overflow! Take a minute to read through [How to Answer](http://stackoverflow.com/questions/how-to-answer) - this looks helpful but it would benefit from some code, consider [edit](http://stackoverflow.com/posts/41385887/edit)-ing that in? – Timothy Truckle Feb 19 '17 at 17:36
  • Timothy Truckle 'im obviously new to stack overflow but a down vote was a little harsh when i'm just trying to help. hopefully this edit was helpful – dag mawi Feb 20 '17 at 11:58