• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

after "calculate", app ignores empty fields

dcparham

Lurker
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:
package com.example;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class AdditionActivity extends Activity
{//Activity BEGIN
 
private EditText amount1;
private EditText amount2;
private EditText amount3;
private TextView tt;
private Button calculate;
String strAmount1 = new String("");
private double val1_height=0;
private double val2_width=0;
private double val3_segments=0;
private double val4_radius=0; //test
private double val5_theta1;
private double val6_theta2;
private double val7_sum;
@SuppressWarnings("unused")
private String result1;
private int fixFlag1 = 0;
boolean flag = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
 
amount1=(EditText)findViewById(R.id.amount1);
amount2=(EditText)findViewById(R.id.amount2);
amount3=(EditText)findViewById(R.id.amount3);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
public void calculate()//was private
{//calculate BEGIN
 try {//#1.3
val1_height=Double.parseDouble(amount1.getText().toString());
val2_width=Double.parseDouble(amount2.getText().toString());
val3_segments=Double.parseDouble(amount3.getText().toString());
 }//try END
 catch(Exception e) {     // ... handle errors ... 
  //throw new RuntimeException(e);
}
  if(val3_segments == 0)
  {//val3 BEGIN
   fixFlag1 = 1;
   // prepare the alert box 
   AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
   // set the message to display 
   alertbox.setMessage("# Segments must be > 0, please reenter."); 
   // add a neutral button to the alert box and assign a click listener 
   alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
   // click listener on the alert box 
   public void onClick(DialogInterface arg0, int arg1) { 
   // the button was clicked 
   } 
   }); 
   // show it 
   alertbox.show();
  }//val3 END
  else {//else BEGIN
  fixFlag1 = 0;
  }//else END
 
if(val2_width == 0)
  {//val2 BEGIN
  fixFlag1 = 1;
   // prepare the alert box 
   AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
   // set the message to display 
   alertbox.setMessage("Width must be > 0, please reenter."); 
   // add a neutral button to the alert box and assign a click listener 
   alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
   // click listener on the alert box 
   public void onClick(DialogInterface arg0, int arg1) { 
    // the button was clicked 
   } 
   }); 
   // show it 
   alertbox.show();
  }//val2 END
  else {//else BEGIN
  fixFlag1 = 0;
  }//else END
if(val1_height == 0)
//if(amount1.getText().toString().equals(""))
{//val1 BEGIN
  fixFlag1 = 1;
  // prepare the aler t box 
  AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
  // set the message to display 
  alertbox.setMessage("Height must be > 0, please reenter."); 
  // add a neutral button to the alert box and assign a click listener 
  alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
  // click listener on the alert box 
  public void onClick(DialogInterface arg0, int arg1) { 
  // the button was clicked 
  } 
  }); 
  // show it 
  alertbox.show();
}//val1 END
else {//else BEGIN
fixFlag1 = 0;
}//else END
//simplified to be simply val1+val2+val3:
val7_sum = val1_height + val2_width + val3_segments;
if(fixFlag1==0){//fixFlag1 BEGIN
tt.setText(Double.toString(val7_sum));
}//fixFlag1 END
else {//else BEGIN
tt.setText("    {LENGTH}"); }//else END
}//calculate END
}//Activity END
 
Back
Top Bottom