Ok so i am bulding this application that calculates 3 input boxes. i am getting a NaN when doing some calculations with 0 values.
here is the main.xml<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/ScrollView01" >
<AbsoluteLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<EditText android:id="@+id/InputNumber01"
android:numeric="integer|decimal" android:imeOptions="actionNext"
android:textStyle="bold" android:hint="Service Call"
android:layout_x="5dip" android:layout_y="5px" android:layout_height="55dip"
android:textSize="20dip" android:layout_width="195dip"
android:selectAllOnFocus="true" android:inputType="numberDecimal"/>
<EditText android:id="@+id/InputNumber02"
android:numeric="integer|decimal" android:inputType="numberDecimal"
android:textStyle="bold" android:hint="Parts" android:layout_x="5dip"
android:layout_y="90px" android:layout_height="55dip"
android:textSize="20dip" android:layout_width="195dip"
android:selectAllOnFocus="true" android:imeOptions="actionNext"/>
<EditText android:id="@+id/InputNumber03" android:numeric="integer|decimal"
android:inputType="numberDecimal" android:textStyle="bold"
android:hint="Labor" android:layout_x="5dip" android:layout_y="175px"
android:layout_height="55dip" android:textSize="20dip"
android:layout_width="195dip" android:selectAllOnFocus="true"
android:imeOptions="actionDone"/>
<Button android:text="Calculate" android:id="@+id/actionButton"
android:textStyle="bold" android:layout_height="50dip"
android:layout_width="fill_parent" android:layout_y="300px"
android:textSize="15dip" />
<TextView android:id="@+id/TextResult" android:layout_height="wrap_content"
android:numeric="integ开发者_如何学编程er|decimal" android:textStyle="bold"
android:layout_width="85dip" android:layout_x="225dip"
android:layout_y="175dip" android:gravity="left" android:textSize="18dip"
android:textColor="#1d9ee2" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView04"
android:text="Total:" android:textStyle="bold" android:gravity="right"
android:layout_width="55dip" android:layout_y="175dip"
android:layout_x="165dip" android:textSize="18dip" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView05"
android:text="Total Labor:" android:layout_y="260dip"
android:layout_width="95dip" android:layout_x="5dip" android:gravity="right" />
<TextView android:text="@+id/TotalLabor"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/TotalLabor" android:layout_y="260dip"
android:layout_x="110dip" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView06"
android:layout_x="5dip" android:text="Total Services:"
android:layout_y="280dip" android:layout_width="95dip"
android:gravity="right" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/TotalServices"
android:text="@+id/TotalServices" android:layout_y="280dip"
android:layout_x="110dip" />
<TextView android:id="@+id/TextView07" android:text="SC Tax %:"
android:layout_y="300dip" android:layout_height="wrap_content"
android:layout_x="5dip" android:layout_width="95dip" android:gravity="right" />
<TextView android:text="@+id/TextView08"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/SCTaxPercent" android:layout_y="300dip"
android:layout_x="110dip" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView08"
android:text="Sales Tax:" android:textStyle="bold" android:layout_y="175dip"
android:layout_x="5dip" android:textSize="18dip"
android:layout_width="wrap_content" android:gravity="right" />
<TextView android:layout_height="wrap_content" android:id="@+id/SalesTax"
android:textStyle="bold" android:numeric="integer|decimal"
android:layout_y="175dip" android:gravity="left"
android:layout_width="65dip" android:textSize="18dip"
android:layout_x="100dip" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView09"
android:text="SC Taxable:" android:layout_y="320dip"
android:layout_x="5dip" android:layout_width="95dip" android:gravity="right" />
<TextView android:layout_height="wrap_content" android:text="@+id/TextView10"
android:layout_width="wrap_content" android:id="@+id/ScTaxable"
android:layout_y="320dip" android:layout_x="110dip" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView10"
android:text="Taxable:" android:layout_y="340dip"
android:layout_width="95dip" android:layout_x="5dip" android:gravity="right" />
<TextView android:layout_height="wrap_content" android:text="@+id/TextView11"
android:layout_width="wrap_content" android:id="@+id/Taxable"
android:layout_y="340dip" android:layout_x="110dip" />
<EditText android:layout_height="wrap_content" android:text="9.50"
android:layout_width="90dip" android:id="@+id/SalesTaxValue"
android:layout_x="5dip" android:layout_y="430dip" android:numeric="integer|decimal"
android:selectAllOnFocus="true" />
<TextView android:layout_height="wrap_content" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_x="5dip"
android:text="Sales Tax Value:" android:layout_y="407dip" />
</AbsoluteLayout>
</ScrollView>
and this is the calculator.java
package com.Eddie.TaxCalculator;
import java.text.NumberFormat;
import com.Eddie.TaxCalculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Calculator extends Activity {
private Button calculateButton;
private EditText inputField01;
private EditText inputField02;
private EditText inputField03;
private EditText salesTaxValue;
private double serviceCall;
private double parts;
private double labor;
private double salestaxvalue;
String zero ="0";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
calculateButton = (Button) findViewById(R.id.actionButton);
calculateButton.setOnClickListener(doSomethingNow);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
private OnClickListener doSomethingNow = new OnClickListener() {
public void onClick(View arg0) {
inputField01 = (EditText) findViewById(R.id.InputNumber01);
inputField02 = (EditText) findViewById(R.id.InputNumber02);
inputField03 = (EditText) findViewById(R.id.InputNumber03);
salesTaxValue = (EditText) findViewById(R.id.SalesTaxValue);
if (inputField01.getText().toString().length()==0)
{
inputField01.setText(zero);
Toast toast = Toast.makeText(getApplicationContext(),
"Service Call TextBox was empty.",
Toast.LENGTH_LONG);
toast.show();
}
if(inputField02.getText().toString().length()==0)
{
inputField02.setText(zero);
Toast toast = Toast.makeText(getApplicationContext(),
"Parts TextBox was empty.",
Toast.LENGTH_LONG);
toast.show();
}
if(inputField03.getText().toString().length()==0)
{
inputField03.setText(zero);
Toast toast = Toast.makeText(getApplicationContext(),
"Labor TextBox was empty.",
Toast.LENGTH_LONG);
toast.show();
}
//service Call is 0
if (inputField01.getText().toString().equals("0"))
{
Toast toast = Toast.makeText(getApplicationContext(),
"Service Call TextBox value is 0.",
Toast.LENGTH_SHORT);
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
//Parts is 0
if (inputField02.getText().toString().equals("0"))
{
//inputField01.setText("10");
Toast toast = Toast.makeText(getApplicationContext(),
"Parts TextBox value is 0.",
Toast.LENGTH_SHORT);
toast.show();
}
//Labor is 0
if (inputField03.getText().toString().equals("0"))
{
//inputField01.setText("10");
Toast toast = Toast.makeText(getApplicationContext(),
"Labor TextBox value is 0.",
Toast.LENGTH_SHORT);
toast.show();
}
serviceCall = Double.parseDouble(inputField01.getText().toString());
parts = Double.parseDouble(inputField02.getText().toString());
labor = Double.parseDouble(inputField03.getText().toString());
salestaxvalue = Double.parseDouble(salesTaxValue.getText().toString());
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setGroupingUsed(false);
TextView totalLabor = (TextView)findViewById(R.id.TotalLabor);
Double totalLaborResults = serviceCall + labor;
String totalLaborCalcInt = numberFormat.format(totalLaborResults).toString();
totalLabor.setText (totalLaborCalcInt);
TextView totalServices = (TextView)findViewById(R.id.TotalServices);
Double totalServicesResults = (parts + labor);
String totalServicesCalcInt = numberFormat.format(totalServicesResults);
totalServices.setText (totalServicesCalcInt);
TextView servicecallTaxpercent = (TextView)findViewById(R.id.SCTaxPercent);
Double scTaxPercentResults = (parts / totalServicesResults);
String serviceCallPercentCalcInt = numberFormat.format(scTaxPercentResults).toString();
servicecallTaxpercent.setText (serviceCallPercentCalcInt);
TextView scTaxable = (TextView)findViewById(R.id.ScTaxable);
Double scTaxableResults = (serviceCall * scTaxPercentResults);
String serviceCallTaxableCalcInt =numberFormat.format (scTaxableResults).toString();
scTaxable.setText (serviceCallTaxableCalcInt);
TextView taxable = (TextView)findViewById(R.id.Taxable);
Double taxableResults = (parts + scTaxableResults);
String taxableCalcInt =numberFormat.format (taxableResults).toString();
taxable.setText (taxableCalcInt);
TextView salesTax = (TextView)findViewById(R.id.SalesTax);
Double salesTaxResults = (taxableResults * salestaxvalue) /100;
String salesTaxCalcInt = numberFormat.format (salesTaxResults).toString();
salesTax.setText (salesTaxCalcInt);
TextView textResult = (TextView)findViewById(R.id.Total);
Double additionResults = (salesTaxResults + parts+ serviceCall + labor);
String TotalCalcInt = numberFormat.format(additionResults).toString();
textResult.setText (TotalCalcInt);
}
};
};
when entering a number in input 1 (service Call) and zeros in the two other inputs i get a NaN Total.
if All input Boxes are zero value i also get a NaN total, NaN sale Tax, NaN scTax % textview (sc is short of Service Call) NaN scTaxable, NaN taxable.
could anyone help me find this but i've been trying for two weeks now. pls help.
The problem is definitely using zero (0) values as Input...
Although, i see you trying to catch such instances with
xxxx.getText().toString().equals("0")
1) This is not a good way, to check for zero...imagine what will happen if i type in 00
or 000
....
2) After you display the Toast
, you go ahead and do the calculations anyway...Use the if-else
branching logic to avoid the calculation when the values are zero.
To Correctly Check for zero...you need to Parse it into a datatype (Double
in your case) and then check for its value....
Goodluck, Cheers!
Divide by zero returns NaN when done on floats/doubles. Check that the divider is not zero before the division.
I mean, dividing by 0 is not something that makes sense, so... just don't do it.
If parts is 0 and labor is 0 then totalServicesResults is 0 and scTaxPercentsResults is infinite.
精彩评论