1. Download our Official Android App: Forums for Android!

after "calculate", app ignores empty fields

Discussion in 'Android Apps & Games' started by dcparham, Aug 1, 2011.

  1. dcparham

    dcparham Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    7
    Joined:
    Jun 17, 2011

    Jun 17, 2011
    7
    1
    6
    Web Developer
    35
    hi All - total noob[java] here [i posted a previous question on this and got no replies, only views/i will close it but first want to see if this is more clear]:

    i have an app on the android written, that adds field1 + field2 + field3 and displays the value in field4. first pass, if ea field is empty, it displays an alert box saying that field is empty. if all 3 fields are empty, then all 3 alerts show up. however if AFTER i hit calculate [which adds 3 non-blank fields] i then clear out [make blank] field1, 2, or 3, or all - it does NOT reach the IF stmts that should be displaying the alerts saying "hey a field is blank" or whatever. see my code below, and THANK YOU for any help you can provide. seems so basic but something in my thinking is not right obviously [app only adds 3 nums, but some other vars in there indicative of original/more complex computation. i simplified it to adding 3 nums, to hide the equation which is the meat of my "unique" app]:

    Code (Text):
    1.  
    2. package com.example;
    3. import java.text.DecimalFormat;
    4. import org.apache.commons.lang.StringUtils;
    5. import android.app.Activity;
    6. import android.app.AlertDialog;
    7. import android.content.DialogInterface;
    8. import android.graphics.Color;
    9. import android.os.Bundle;
    10. import android.widget.EditText;
    11. import android.widget.TextView;
    12. import android.widget.Button;
    13. import android.widget.Toast;
    14. import android.view.View;
    15. public class AdditionActivity extends Activity
    16. {//Activity BEGIN
    17.  
    18. private EditText amount1;
    19. private EditText amount2;
    20. private EditText amount3;
    21. private TextView tt;
    22. private Button calculate;
    23. String strAmount1 = new String("");
    24. private double val1_height=0;
    25. private double val2_width=0;
    26. private double val3_segments=0;
    27. private double val4_radius=0; //test
    28. private double val5_theta1;
    29. private double val6_theta2;
    30. private double val7_sum;
    31. @SuppressWarnings("unused")
    32. private String result1;
    33. private int fixFlag1 = 0;
    34. boolean flag = true;
    35. /** Called when the activity is first created. */
    36. @Override
    37. public void onCreate(Bundle savedInstanceState)
    38. {
    39. super.onCreate(savedInstanceState);
    40. setContentView(R.layout.main);
    41. initControls();
    42. }
    43. private void initControls()
    44. {
    45.  
    46. amount1=(EditText)findViewById(R.id.amount1);
    47. amount2=(EditText)findViewById(R.id.amount2);
    48. amount3=(EditText)findViewById(R.id.amount3);
    49. tt=(TextView)findViewById(R.id.tt);
    50. calculate=(Button)findViewById(R.id.calculate);
    51. calculate.setOnClickListener(new Button.OnClickListener()
    52. {public void onClick
    53. (View v) { calculate();}});
    54. }
    55. public void calculate()//was private
    56. {//calculate BEGIN
    57.  try {//#1.3
    58. val1_height=Double.parseDouble(amount1.getText().toString());
    59. val2_width=Double.parseDouble(amount2.getText().toString());
    60. val3_segments=Double.parseDouble(amount3.getText().toString());
    61.  }//try END
    62.  catch(Exception e) {     // ... handle errors ...
    63.   //throw new RuntimeException(e);
    64. }
    65.   if(val3_segments == 0)
    66.   {//val3 BEGIN
    67.    fixFlag1 = 1;
    68.    // prepare the alert box
    69.    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    70.    // set the message to display
    71.    alertbox.setMessage("# Segments must be > 0, please reenter.");
    72.    // add a neutral button to the alert box and assign a click listener
    73.    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
    74.    // click listener on the alert box
    75.    public void onClick(DialogInterface arg0, int arg1) {
    76.    // the button was clicked
    77.    }
    78.    });
    79.    // show it
    80.    alertbox.show();
    81.   }//val3 END
    82.   else {//else BEGIN
    83.   fixFlag1 = 0;
    84.   }//else END
    85.  
    86. if(val2_width == 0)
    87.   {//val2 BEGIN
    88.   fixFlag1 = 1;
    89.    // prepare the alert box
    90.    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    91.    // set the message to display
    92.    alertbox.setMessage("Width must be > 0, please reenter.");
    93.    // add a neutral button to the alert box and assign a click listener
    94.    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
    95.    // click listener on the alert box
    96.    public void onClick(DialogInterface arg0, int arg1) {
    97.     // the button was clicked
    98.    }
    99.    });
    100.    // show it
    101.    alertbox.show();
    102.   }//val2 END
    103.   else {//else BEGIN
    104.   fixFlag1 = 0;
    105.   }//else END
    106. if(val1_height == 0)
    107. //if(amount1.getText().toString().equals(""))
    108. {//val1 BEGIN
    109.   fixFlag1 = 1;
    110.   // prepare the aler t box
    111.   AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    112.   // set the message to display
    113.   alertbox.setMessage("Height must be > 0, please reenter.");
    114.   // add a neutral button to the alert box and assign a click listener
    115.   alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
    116.   // click listener on the alert box
    117.   public void onClick(DialogInterface arg0, int arg1) {
    118.   // the button was clicked
    119.   }
    120.   });
    121.   // show it
    122.   alertbox.show();
    123. }//val1 END
    124. else {//else BEGIN
    125. fixFlag1 = 0;
    126. }//else END
    127. //simplified to be simply val1+val2+val3:
    128. val7_sum = val1_height + val2_width + val3_segments;
    129. if(fixFlag1==0){//fixFlag1 BEGIN
    130. tt.setText(Double.toString(val7_sum));
    131. }//fixFlag1 END
    132. else {//else BEGIN
    133. tt.setText("    {LENGTH}"); }//else END
    134. }//calculate END
    135. }//Activity END
    136.  
     

    Advertisement

Share This Page

Loading...