1

I'm trying to wrote android program which displays Greatest Common Divisor of two integers specified in two different EditText fields. First I've done it with button, everything worked (you can see onclick listener commented out in code below). Now I want to do this: app checks when both EditTexts are not empty and then automatically starts calculating and shows gcd. Buty app crashes when I start typing in any of EditText fields. Also I tried to add TextChangeListener only on one of EditTexts. Everything is good until I delete all input from one of the fields, then app crashes again. I'm only starting to understand android development and made this app mostly by modifying examples found on internet so maybe I did something wrong... Can anyone help me? Thanks

MainActivity.java

    public class MainActivity extends Activity 

{
    EditText a;
    EditText b;
    TextView gcdResult;
    Button calculateGcd;
    int a, b, gcdValue

    TextWatcher textWatcher = new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s){}

        @Override
        public void beforeTextChanged(CharSequence s,int start, int count, int after){}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){

            AutoCalculateGcd();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        a = (EditText)findViewById(R.id.aText1);
        b = (EditText)findViewById(R.id.bText1);
        gcdResult = (TextView)findViewById(R.id.resultTextView1);
        calculateGcd = (Button)findViewById(R.id.calcButton1);

        /* calculateGcd.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                AutoCalculateRatio();
            }
        });*/

        a.addTextChangedListener(textWatcher);
        b.addTextChangedListener(textWatcher);

    }



    //Euclidean alghorithm to find gcd
    public static int gcd(int a, int b) {
        if (b == 0) return w;
        else return gcd(b a % b);

        }
    public static boolean isInputNotEmpty(EditText a, EditText b){
        String a = a.getText().toString();
        String b = b.getText().toString();
        if(a.equals("") && b.equals("") ){
             return false;
    }
    else{
        return true;
    }

    }
    public void AutoCalculateGcd(){
        if(isInputNotEmpty(a, b)){
            a = Integer.parseInt(width.getText().toString());
            b = Integer.parseInt(height.getText().toString());
            gcdValue = gcd(a, b);
            ratioResult.setText(Integer.toString(gcdValue));
        }
        else{
            //Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();
        }
    }
}
Vykintazo
  • 163
  • 2
  • 11
  • 1
    Does that even compile? You have duplicate variable names - `EditText a, int a, EditText b, int b` – mihail Jul 20 '17 at 14:44
  • 1
    This can not compile, we cant write your program for you. I´m happy to help you with a problem, but building your app... No, thanks. – Tacolibre Jul 20 '17 at 15:25
  • @mihail I'm sorry. This may be pretty strange, but the thing is I wrote this question while getting home from vacation, and I wrote source code from memory. Wanted to have a solution when I get home. I remembered all main points of code and tried to reconstruct it, well as I can see I missed some things... – Vykintazo Jul 20 '17 at 17:17

2 Answers2

3

Actually, you should replace

public static boolean isInputNotEmpty(EditText a, EditText b) {
    String a = a.getText().toString();
    String b = b.getText().toString();
    if (a.equals("") && b.equals("")) {
         return false;
    }
    else {
        return true;
    }
}

with

public static boolean isInputNotEmpty(EditText a, EditText b) {
    String a = a.getText().toString();
    String b = b.getText().toString();
    if (a.equals("") || b.equals("")) {
         return false;
    }
    else {
        return true;
    }
}

Or even

public static boolean isInputNotEmpty(EditText a, EditText b) {
    return !(a.getText().toString().isEmpty() || b.getText().toString().isEmpty());
}

Because you want to know if any ( || ) of them is empty, not if both (&&) are.

Szymon Chaber
  • 2,076
  • 16
  • 19
2

It might help if you post the stacktrace, but my guess is that you are getting a NumberFormatException from the Integer.parseInt() calls. One approach would be to do something like:

try {
        a = Integer.parseInt(width.getText().toString());
        b = Integer.parseInt(height.getText().toString());
        gcdValue = gcd(a, b);
        ratioResult.setText(Integer.toString(gcdValue));
} catch ( NumberFormatException e) {
        ratioResult.setText("N/A")
} 
cwbowron
  • 1,015
  • 6
  • 10