1

I am a 11 year old app programer and I am making a calculator app. I want to add a percent button, so the user can find percantage, but when I enter 50% of 100, it gives me 0, even when the correct answer is 50. It also says wrong answers for other equations. Please help! This is my java code:

package com.example.lenovouser.calculatoraghav;
//IMPORT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
// ASSIGNING THE NAMES OR VARIABLES
TextView result;
EditText number1, number2, txt;
Button 
add,subtract,divide,multiply,squareroot,square,clear,Exponent,percent;
double resultnum;
int num1,num2,num3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //ASSIGNING THE VARIABLES TO THE ANDROID WIDGETS.
    result = (TextView)findViewById(R.id.sum);
    number1 = (EditText)findViewById(R.id.number1);
    number2 = (EditText)findViewById(R.id.number2);
    txt = (EditText)findViewById(R.id.txt);
    add = (Button)findViewById(R.id.button);
    subtract = (Button)findViewById(R.id.button2);
    multiply = (Button)findViewById(R.id.button3);
    divide = (Button)findViewById(R.id.button4);
    squareroot =(Button)findViewById(R.id.button6);
    square = (Button)findViewById(R.id.button5);
    clear = (Button)findViewById(R.id.button8);
    Exponent = (Button)findViewById(R.id.button9);
    percent = (Button)findViewById(R.id.button10);



    add.setOnClickListener(new View.OnClickListener(){

@Override
    public void onClick(View v){
        //ADD
        num1 = Integer.parseInt(number1.getText() .toString());
        num2 = Integer.parseInt(number2.getText() .toString());
        resultnum = num1 + num2;
        result.setText(String.valueOf(resultnum));
}
});




    subtract.setOnClickListener(new View.OnClickListener(){
        //SUBTRACT
        @Override
        public void onClick(View v){

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 - num2;
            result.setText(String.valueOf(resultnum));
        }
    });




    multiply.setOnClickListener(new View.OnClickListener(){
        //MULTIPLY
        @Override
        public void onClick(View v){

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 * num2;
            result.setText(String.valueOf(resultnum));
        }
    });





    divide.setOnClickListener(new View.OnClickListener(){
        //DIVIDE
        @Override
        public void onClick(View v){

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 / num2;
            result.setText(String.valueOf(resultnum));
        }
    });


    squareroot.setOnClickListener(new View.OnClickListener(){
        //SQUAREROOT
        @Override
        public void onClick(View v){

            num1 = Integer.parseInt(number1.getText() .toString());

            resultnum = (Math.sqrt(num1));
            result.setText(String.valueOf(resultnum));
        }
    });


    square.setOnClickListener(new View.OnClickListener(){
        //SQUARE
        @Override
        public void onClick(View v){

            num1 = Integer.parseInt(txt.getText() .toString());

            resultnum = num1*num1;
            result.setText(String.valueOf(resultnum));
        }
    });


    clear.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
        //CLEAR
            num1 = (0);
            num2 = (0);
            num3 = (0);
            number1.setText("");
            number2.setText("");
            txt.setText("");
            resultnum = 0;
            result.setText(String.valueOf(resultnum));
        }
    });


    Exponent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //EXPONENT
            int p = Integer.parseInt(number1.getText() .toString());
            int e = Integer.parseInt(number2.getText() .toString());
            resultnum = (Math.pow((double)p, (double)e));
            result.setText(String.valueOf(resultnum));
        }
    });



    percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1/num2;
            resultnum = resultnum * 100;
            result.setText(String.valueOf(resultnum));
        }
    });
}}

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
    android:id="@+id/number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    android:ems="10"
    android:hint="number1"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="92dp"
    android:ems="10"
    android:hint="number2"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/sum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="24dp"
    android:text="result"
    android:textSize="25sp"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp"
    android:background="@android:color/holo_blue_bright"
    android:text="ADD-+"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/sum" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="22dp"
    android:background="@android:color/holo_red_light"
    android:text="SUBTRACT--"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="23dp"
    android:background="@android:color/holo_green_dark"
    android:text="MULTIPLY-X"
    app:layout_constraintStart_toStartOf="@+id/button2"
    app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="52dp"
    android:text="DIVIDE-/"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/button3"
    tools:background="@android:color/holo_orange_light" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="17dp"
    android:layout_marginRight="17dp"
    android:text="Square"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:text="Square Root"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<EditText
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="square number"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button4" />

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="10dp"
    android:text="CLEAR"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="108dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="Exponent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginBottom="8dp"
    android:text="Percent"
    app:layout_constraintBottom_toTopOf="@+id/txt"
    app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

So far, the calculator preforms well in addition, subtraction, ect. It is the percentage thats bugging me. This my percent function:

        percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1/num2;
            resultnum = resultnum * 100;
            result.setText(String.valueOf(resultnum));
        }
    });
}}

5 Answers5

1

Ray is on the right track, but even using a double will give you strange behavior because the underlying system is bit based and cannot represent base 10 decimals perfectly. If you multiply by 100 first, you will be working in whole numbers and get whole number results. Try this: 100 * num1 / num2

John Camerin
  • 545
  • 4
  • 12
1

Integer is not good choice to do mathematical calculation, please consider using Double for more precision.

It is because, despite the fact that Integer got range between -2,147,483,648 .. 2,147,483,647, it cannot take values other then whole number.

The result of dividing 5 by 100 in Integer data-type will not be 0,05, but 0 since there will be rounding happening under hood. Then, when you multiply 0 by 100 you still got 0.

Wild_Owl
  • 360
  • 1
  • 7
0

I think your main problem that you perform calculation of percentage in Integer type. It's better to use double because in Java when you divide a smaller integer to bigger integer you will get 0.

Try to replace:

Double percentage = Double.valueOf(number1.getText().toString())
Integer num2 = Integer.parseInt(number2.getText().toString());
Double resultnum = percentage * num2 / 100;

It's just a draft sample. For sure, it would be better to perform all calculations in Double for such calculator app.

If your final goal to build a reliable calculator you can review even more precise data type: BigDecimal

Ray
  • 1,705
  • 7
  • 51
  • 83
0

Here is your issue :

resultnum = num1/num2;

When you divide an integer by an integer, you're making an integer division. That means that you need to cast your numbers into doubles before operating your calculation, otherwise you'll get the integer part of the result. Of couse, 0 * 100 equals zero.

Try this :

resultnum = num1/(num2*1.0d)

When you divide by a double, you get a double result, so you only have to cast your diviser into a double.

Whole Brain
  • 531
  • 5
  • 15
0

Dividing an int number by another int number will give the result of the entire division.

In the case of a percentage, your result should always be 0 if you're expecting a percentage between 0 and 100%.

When implementing division or multiplication operation, you should use float/double variables ;)

SinLey
  • 57
  • 6