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

I am working on a Android App. Everything is okay and everything does work but I want to short some commands which are usen very often. Here is a part of my code:

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));
}

public void ButtonKlick2 (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 = (zahl4 - zahl3) / (zahl2 - zahl1);

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

}

As you can see, there are a lot of commands which are used more than one time. It start from

double zahl1;

to

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

The code between these lines are always the same. I dont want to repeat the lines in my new public void. Is there a method to short my code? I did think about procedures...

Answers


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


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;
 }

Need Your Help

Bind checkbox on DB initial null value

c# asp.net checkbox bind

I need to bind a checkBox but with this line of code,

Using inner join and group by

mysql group-by inner-join

I have a restaurants. So I have a "menu" table and an "orders" table. I want to find when a given dish was most recently ordered. So I know I have to use group by dishId at some point, but everythi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.