Reputation: 1120
I am trying to do 1 of 2 things. IF all of my EditText are input with some data where length>0 then it should progress to my math equations and put the data into EditText dispMinPmt. If there is a NULL value EditText field, it should do the Toast at the end. Currently, neither are occurring. There are also no errors to display.
Here is my .java:
@SuppressLint("ShowToast")
public void ButtonOnClick(View v)
{
TextView dispMinPmt = (TextView)findViewById(R.id.dispMinPmt);
Intent intent = new Intent(this, MainActivity.class);
switch (v.getId()) {
case R.id.calculate1:
Toast.makeText(getApplicationContext(), "Check 1", Toast.LENGTH_SHORT).show();
EditText nickname = (EditText) findViewById(R.id.editNickname);
if (nickname.getText().toString().length()>0)
{
String debtNickname = nickname.getText().toString();
EditText origBal = (EditText) findViewById(R.id.editOrigBal);
if (origBal.getText().toString().length()>0)
{
String myOrigBal = origBal.getText().toString();
Double originalBalance = Double.parseDouble(myOrigBal);
EditText myAPR = (EditText) findViewById(R.id.editAPR);
if (myAPR.getText().toString().length()>0)
{
String myEditValue2 = myAPR.getText().toString();
Double apr = Double.parseDouble(myEditValue2);
EditText myOrigTerm = (EditText) findViewById(R.id.editOrigTerm);
if (myOrigTerm.getText().toString().length()>0)
{
String myEditValue3 = myOrigTerm.getText().toString();
Double originalTerm = Double.parseDouble(myEditValue3);
double r = apr/1200;
double r1 = Math.pow(r+1,originalTerm);
Double minPmt = (double) ((r+(r/(r1-1))) * originalBalance);
DecimalFormat df = new DecimalFormat("#.##");
minPmt = Double.valueOf(df.format(minPmt));
dispMinPmt.setText("" + String.valueOf(minPmt) + "Mo.");
}
}
}
}
else
{
Toast.makeText(getApplicationContext(), "Please complete all fields", Toast.LENGTH_SHORT).show();
}
break;
}
}
Here is my layout (.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DebtInput" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textNickname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Debt Nickname" />
<EditText
android:id="@+id/editNickname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:text="Car Loan"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textOrigBal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Original Balance" />
<EditText
android:id="@+id/editOrigBal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="10000.00"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textAPR"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="APR (%)" />
<EditText
android:id="@+id/editAPR"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="0.00"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textOrigTerm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Loan Term (Months)" />
<EditText
android:id="@+id/editOrigTerm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:text="60"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textOutBal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Outstanding Balance" />
<EditText
android:id="@+id/editOutBal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="1000.00"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/calcButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/calculate1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Calculate" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textMinPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Minimum Payment" />
<TextView
android:id="@+id/dispMinPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textAddPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Additional Payment" />
<EditText
android:id="@+id/editAddPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="100.00"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/calcButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/calculate2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Calculate" />
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textNewPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New Payment" />
<TextView
android:id="@+id/dispNewPmt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:id="@+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="@+id/clear"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Clear" />
</TableRow>
Upvotes: 1
Views: 201
Reputation: 500
Try using the StringUtils.isEmpty(nicknameStr)
method, rather than nicknameStr.length > 0
.
Upvotes: 0
Reputation: 16771
Your IF statements is overly nested. This is a bad practice. Also, your else statement will only trigger if the 1st condition is not met. If any of the subsequent conditions are not met, nothing will happen.
I tool the liberty to tidy your code a bit:
public void ButtonOnClick(View v)
{
TextView dispMinPmt = (TextView)findViewById(R.id.dispMinPmt);
Intent intent = new Intent(this, MainActivity.class);
switch (v.getId()) {
case R.id.calculate1:
Toast.makeText(getApplicationContext(), "Check 1", Toast.LENGTH_SHORT).show();
EditText nickname = (EditText) findViewById(R.id.editNickname);
EditText origBal = (EditText) findViewById(R.id.editOrigBal);
EditText myAPR = (EditText) findViewById(R.id.editAPR);
EditText myOrigTerm = (EditText) findViewById(R.id.editOrigTerm);
String nicknameStr = nickname.getText().toString();
String origBalStr = origBal.getText().toString();
String myAPRStr = myAPR.getText().toString();
String myOrigTermStr = myOrigTerm.getText().toString();
double origTermValue;
boolean origTermIsParsable;
try
{
origTermValue = Double.parseDouble(origBalStr);
origTermIsParsable = true;
}
catch (NumberFormatException e)
{
origTermIsParsable = false;
}
if (nicknameStr.length() > 0 && origBalStr.length() > 0 && myAPRStr.length() > 0 && myOrigTermStr.length() > 0 && origTermIsParsable)
{
Double originalTerm = origTermValue;
double r = apr/1200;
double r1 = Math.pow(r+1,originalTerm);
Double minPmt = (double) ((r+(r/(r1-1))) * originalBalance);
DecimalFormat df = new DecimalFormat("#.##");
minPmt = Double.valueOf(df.format(minPmt));
dispMinPmt.setText("" + String.valueOf(minPmt) + "Mo.");
}
else
{
Toast.makeText(getApplicationContext(), "Please complete all fields", Toast.LENGTH_SHORT).show();
}
break;
}
}
Generally speaking, nesting so many IFs inside each other is something you should always try to avoid. In the event that you have two if's one inside the other, eliminate one of them, and combine the conditions with an "AND" statement.
Hope this helps :)
Upvotes: 2
Reputation: 6925
Try adding trim() It will remove all spaces included , like this
nickname.getText().toString().trim().length()>0
Also put this line in your onCreate Method
TextView dispMinPmt = (TextView)findViewById(R.id.dispMinPmt);
Upvotes: 0
Reputation: 3585
int value = 0;
value= Integer.parseInt(edittext.getText().toString());
and check if value>0 .
This is the case to check when your edit text data is integer value and not the string.
Upvotes: 0
Reputation: 23503
Your problem is that you are trying to compare a string using >
and that won't work. You either need to make it into an int
; or, you need to use the equals
method to compare strings. Using the equals
you won't be able to use greater-than or less-than though.
Upvotes: 1