do-while loop in android app

4k views Asked by At

I'm about to program my first app. I would like to use a do-while loop. I want that a button calculates something out of edittexts. After That I want to ask the user if he or she wants to calculate something again. I think a do-while-loop would be great, also because i haven't one in my app yet. The problem is that I'm not sure how to do that? The way I tried to do that always says that i need to delete the token while.. Maybe it's because it's in a if/else instruction. Another problem is that I don't know how to program the while condition. is it possible with the alert dialog?

My question is: What's wring with my do-while loop? What do I need to write in the while condition to control if the user clicked ok in the altert dialog? How can I add a ''no'' button to the alert dialog?

 @Override
    public void onClick(View v) {
        if(v==buttonspritkosten){

do{         

            //Variablen erzeugen
            TextView textViewPersonenzahl;
            TextView textViewStrecke;
            TextView textViewVerbrauch;
            TextView textViewPreis;
            double berechnung;
            String personenzahl;
            String strecke;
            String verbrauch;
            String preis;



            // Eingabefelder auslesen       
            textViewPersonenzahl = (TextView) findViewById(R.id.editpersonen);
            textViewStrecke = (TextView) findViewById(R.id.editstrecke);
            textViewVerbrauch = (TextView) findViewById(R.id.editverbrauch);
            textViewPreis = (TextView) findViewById(R.id.editpreis);
            //In strings umwandeln
            personenzahl=textViewPersonenzahl.getText().toString();
            strecke=textViewStrecke.getText().toString();
            verbrauch=textViewVerbrauch.getText().toString();
            preis=textViewPreis.getText().toString();


                    // Prüfen ob Felder ausgefüllt wurden mit der Methode Felderprüfung
                    if (Felderprüfung(personenzahl) == true && Felderprüfung(strecke) == true && Felderprüfung(verbrauch)== true && Felderprüfung(preis) == true) {


                        //Prüfen ob sie Strecke zu kurz ist und ggf. AltertDialog starten   
                        if(Double.parseDouble(strecke)< 4){

                            String message = "Schonen Sie doch lieber die Umwelt und gehen zu Fuß! ;)";
                            new AlertDialog.Builder(Berechnungsactivity.this)
                            .setTitle("Umweltschutz")
                            .setMessage(message)
                            .setPositiveButton("ok", null )
                            .show();}


                    berechnung= ((((Double.parseDouble(strecke))/100)*(Double.parseDouble(verbrauch)))*(Double.parseDouble(preis)))/(Double.parseDouble(personenzahl));

                    spritkosten.setText("Die Spritkosten betragen: "+ berechnung + " € pro Person");


            }else{
                Toast einToast = Toast.makeText(this, "Bitte füllen Sie die Felder aus, damit Calci rechnen kann!", Toast.LENGTH_LONG);
                einToast.show();
            }

                    String message = "Möchten Sie eine weitere Berechnung durchführen?";
                    new AlertDialog.Builder(Berechnungsactivity.this)
                    .setMessage(message)
                    .setPositiveButton("ok", null )
                    .show();    



}while()



            }

        else if (v == buttonabbrechen) {
            // sollte der User Abbrechen drücken, soll die App beendet werden
            finish();


        }


    }

The failtures are:

Syntax error on token(s), misplaced construct(s) Thank you for your help!

Syntax error on tokens, delete these tokens

1

There are 1 answers

4
Moritz On BEST ANSWER

You need to catch if the user presses something like "no":

boolean someBool = true;

.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   someBool = false;
               }
           });
do{
   some code
}while(someBool);

You can set a boolean if the user presses "no" to false and check inside the while condition if this is still true. If now, then the while loop will exiting.

See more here for AlertDialog and how to catch user actions: https://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment