Manh Khôi Duong
Manh Khôi Duong

Reputation: 173

Java: How can I shorten codes which are usen often?

I am working on an Android App. Everything seems to work fine but I want to make my code shorter than it is now. You may get the idea that a lot of lines are repetitive:

public void ButtonKlick (View view) {


    double zahl1;
    double zahl2;
    double zahl3;
    double zahl4;
    double Ergebnis = 0;
    EditText Feld1 = (EditText)findViewById(R.id.zahl1);
    EditText Feld2 = (EditText)findViewById(R.id.zahl2);
    EditText Feld3 = (EditText)findViewById(R.id.zahl3);
    EditText Feld4 = (EditText)findViewById(R.id.zahl4);
    EditText FeldErgebnis = (EditText)findViewById(R.id.etErgebnis);
    if (Feld1.getText().toString().length() == 0 ) {
        return;
    }
    if (Feld2.getText().toString().length() == 0 ) {
        return;
    }
    if (Feld3.getText().toString().length() == 0 ) {
        return;
    }
    if (Feld4.getText().toString().length() == 0 ) {
        return;
    }
    zahl1 = Double.parseDouble(Feld1.getText().toString());
    zahl2 = Double.parseDouble(Feld2.getText().toString());
    zahl3 = Double.parseDouble(Feld3.getText().toString());
    zahl4 = Double.parseDouble(Feld4.getText().toString()); 


    Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2));

    FeldErgebnis.setText(String.valueOf(Ergebnis));
}

It all starts in both methods from:

double zahl1;

to

zahl4 = Double.parseDouble(Feld4.getText().toString());

Is there any specific way to get rid of those same lines?

Upvotes: 1

Views: 200

Answers (2)

Blackbelt
Blackbelt

Reputation: 157437

write an utility method like:

private double getDouble(EditText tv) {
   return Double.parseDouble(tv.getText().toString());
}

and call it like:

zahl4 = getDouble(Field4);

EDIT:

private double getDouble(int viewId) {
   View view = findViewById(viewId);
   double toReturn = 0;
   // instanceOf returns false for null values
   if (view instanceOf EditText) 
      toReturn = Double.parseDouble(view.getText().toString());
   return toReturn;
 }

Upvotes: 2

codeMagic
codeMagic

Reputation: 44571

Yes. Instead of having different functions for each Button just use one and get the id of theButton. I assume you are declaring theonClick` in your xml. Just declare the same function if these all do the same thing

public void ButtonKlick (View view) {

 switch (view.getId())
{
    case (R.id.button1Id):
    // do specific stuff for button with id button1Id like
    Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2));
    break;
    case (R.id.button2Id):
     Ergebnis = (zahl4 - zahl3) / (zahl2 - zahl1);
     break;
     ...
}
double zahl1;
double zahl2;
double zahl3;
double zahl4;
double Ergebnis = 0;
EditText Feld1 = (EditText)findViewById(R.id.zahl1);
EditText Feld2 = (EditText)findViewById(R.id.zahl2);
EditText Feld3 = (EditText)findViewById(R.id.zahl3);
EditText Feld4 = (EditText)findViewById(R.id.zahl4);
EditText FeldErgebnis = (EditText)findViewById(R.id.etErgebnis);
if (Feld1.getText().toString().length() == 0 ) {
    return;
}
if (Feld2.getText().toString().length() == 0 ) {
    return;
}
if (Feld3.getText().toString().length() == 0 ) {
    return;
}
if (Feld4.getText().toString().length() == 0 ) {
    return;
}
zahl1 = Double.parseDouble(Feld1.getText().toString());
zahl2 = Double.parseDouble(Feld2.getText().toString());
zahl3 = Double.parseDouble(Feld3.getText().toString());
zahl4 = Double.parseDouble(Feld4.getText().toString()); 


Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2));

FeldErgebnis.setText(String.valueOf(Ergebnis));

}

If I understand what you want then this would keep from needing different methods to do the same thing

Upvotes: 1

Related Questions