A method that I added to my program doesn't return the desired result

Viewed 56

I am making an android app that can calculate numbers and fractions. Here's the code:

package com.dkapps.shownamenow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonAdd = findViewById(R.id.button_add);
        Button buttonSub = findViewById(R.id.button_subtract);
        Button buttonMul = findViewById(R.id.button_multiply);
        Button buttonDiv = findViewById(R.id.button_divide);
        final EditText editTextNum1 = findViewById(R.id.textview_number_1);
        final EditText editTextNum2 = findViewById(R.id.textview_number_2);
        final TextView textViewResult = findViewById(R.id.textView_result);
        final Switch simplify = findViewById(R.id.switch_simplify);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fraction_1 = editTextNum1.getText().toString();
                String fraction_2 = editTextNum2.getText().toString();
                int Num1, Den1, Num2, Den2;
                if (fraction_1.contains("/")){
                    Num1 = Integer.parseInt(getFraction(fraction_1)[0]);
                    Den1 = Integer.parseInt(getFraction(fraction_1)[1]);
                } else {
                    Num1 = Integer.parseInt(fraction_1);
                    Den1 = 1;
                }
                if (fraction_2.contains("/")){
                    Num2 = Integer.parseInt(getFraction(fraction_2)[0]);
                    Den2 = Integer.parseInt(getFraction(fraction_2)[1]);
                } else {
                    Num2 = Integer.parseInt(fraction_2);
                    Den2 = 1;
                }

                int commonDen = getLCM(Den1, Den2);
                Num1 = (commonDen/Den1)*Num1;
                Num2 = (commonDen/Den2)*Num2;
                int addNum = Num1 + Num2;

                if (simplify.isChecked()){
                    String result = simplify(addNum, commonDen);
                    result = checkFor1(result);
                    result = checkForWhole(result);


                    textViewResult.setText(result);
                } else {
                    String result = addNum + "/" + commonDen;
                    textViewResult.setText(result);
                }
            }
        });

        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fraction_1 = editTextNum1.getText().toString();
                String fraction_2 = editTextNum2.getText().toString();

                int Num1, Den1, Num2, Den2;
                if (fraction_1.contains("/")){
                    Num1 = Integer.parseInt(getFraction(fraction_1)[0]);
                    Den1 = Integer.parseInt(getFraction(fraction_1)[1]);
                } else {
                    Num1 = Integer.parseInt(fraction_1);
                    Den1 = 1;
                }
                if (fraction_2.contains("/")){
                    Num2 = Integer.parseInt(getFraction(fraction_2)[0]);
                    Den2 = Integer.parseInt(getFraction(fraction_2)[1]);
                } else {
                    Num2 = Integer.parseInt(fraction_2);
                    Den2 = 1;
                }

                int commonDen = getLCM(Den1, Den2);
                Num1 = (commonDen/Den1)*Num1;
                Num2 = (commonDen/Den2)*Num2;
                int subtractNum = Num1 - Num2;

                if (simplify.isChecked()){
                    String result = simplify(subtractNum, commonDen);
                    result = checkFor1(result);
                    result = checkForWhole(result);


                    textViewResult.setText(result);
                } else {
                    String result = subtractNum + "/" + commonDen;
                    textViewResult.setText(result);
                }
            }
        });

        buttonMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fraction_1 = editTextNum1.getText().toString();
                String fraction_2 = editTextNum2.getText().toString();

                int Num1, Den1, Num2, Den2;
                if (fraction_1.contains("/")){
                    Num1 = Integer.parseInt(getFraction(fraction_1)[0]);
                    Den1 = Integer.parseInt(getFraction(fraction_1)[1]);
                } else {
                    Num1 = Integer.parseInt(fraction_1);
                    Den1 = 1;
                }
                if (fraction_2.contains("/")){
                    Num2 = Integer.parseInt(getFraction(fraction_2)[0]);
                    Den2 = Integer.parseInt(getFraction(fraction_2)[1]);
                } else {
                    Num2 = Integer.parseInt(fraction_2);
                    Den2 = 1;
                }

                int multiplyNum = Num1 * Num2;
                int multiplyDen = Den1 * Den2;

                if (simplify.isChecked()){
                    String result = simplify(multiplyDen, multiplyDen);
                    result = checkFor1(result);
                    result = checkForWhole(result);


                    textViewResult.setText(result);
                } else {
                    String result = multiplyNum + "/" + multiplyDen;
                    textViewResult.setText(result);
                }
            }
        });

        buttonDiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fraction_1 = editTextNum1.getText().toString();
                String fraction_2 = editTextNum2.getText().toString();

                int Num1, Den1, Num2, Den2;
                if (fraction_1.contains("/")){
                    Num1 = Integer.parseInt(getFraction(fraction_1)[0]);
                    Den1 = Integer.parseInt(getFraction(fraction_1)[1]);
                } else {
                    Num1 = Integer.parseInt(fraction_1);
                    Den1 = 1;
                }
                if (fraction_2.contains("/")){
                    Num2 = Integer.parseInt(getFraction(fraction_2)[0]);
                    Den2 = Integer.parseInt(getFraction(fraction_2)[1]);
                } else {
                    Num2 = Integer.parseInt(fraction_2);
                    Den2 = 1;
                }

                int newNum2 = Den2;
                int newDen2 = Num2;
                int divideNum = Num1 * newNum2;
                int divideDen = Den1 * newDen2;

                if (simplify.isChecked()){
                    String result = simplify(divideNum, divideDen);
                    result = checkFor1(result);
                    result = checkForWhole(result);


                    textViewResult.setText(result);
                } else {
                    String result = divideNum + "/" + divideDen;
                    textViewResult.setText(result);
                }
            }
        });
    }
    public static String checkFor1(String fraction){
        String result;
        String[] split = fraction.split("/");
        if (split[1] == "1"){
            result = split[0];
        } else {
            result = fraction;
        }
        return result;
    }
    public static String simplify(int a, int b){
        int commonDivisor = getHCF(a,b);
        a /= commonDivisor;
        b /= commonDivisor;
        String result = a + "/" + b;
        return result;
    }
    public static String[] getFraction(String fraction_string){
        String[] split = fraction_string.split("/");
        return split;
    }
    public static int getHCF(int a, int b) {
        int rem = 1;
        int dend = Math.max(a, b);
        int sor = Math.min(a, b);
        do {
            rem = dend % sor;
            dend = sor;
            sor = rem;


        } while (rem > 0);


        return dend;
    }
    public static int getLCM(int a, int b){
        int hcf = getHCF(a, b);
        int lcm = (a*b)/hcf;
        return lcm;
    }
    public static String checkForWhole(String a)    {
        String result;
        if (a.contains("/")){
            String[] split = a.split("/");
            if (split[0] == split[1]){
                result = "1";
            } else {
                result = a;
            }
        } else {
            result = a;
        }

        return result;
    }
}

I want the checkFor1 method to check if a fraction has the denominator as one. If it does, it will remove the denominator and if it doesn't, it will leave the fraction as it is. But it is not doing that. When I run it and enter whole numbers (example: 1 and 2) in the editTexts, the result is 3/1. I don't understand why this is happening. Is there a problem in my code?

1 Answers

You are doing checkfor1 only if simplify is checked make sure you are checking it. Other wise you can do checkfor1 in the else case also, And also you are checking with == on checkfor1 it will check if the two references are same not the actual string you have to use split[1].equals("1")

Right implementation of checkFor1

public static String checkFor1(String fraction) {
        String result;
        String[] split = fraction.split("/");
        if (split[1].equals("1")) {
            result = split[0];
        } else {
            result = fraction;
        }
        return result;
    }
Related