Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am using android studio along with java coding to produce an app that easily splits a restaurant bill for a group of people. My
I am using android studio along with java coding to produce an app that easily splits a restaurant bill for a group of people. My problem is that I am getting a divide by zero error and cannot figure out how to fix it.
SecondActivity.java import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import java.text.DecimalFormat; public class SecondActivity extends Activity { int billTotals; int groupSize; double percentTotal; double individualTotal; int groupsSize; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); final EditText billTotal = (EditText) findViewById(R.id.billTotal); final EditText groupSize = (EditText) findViewById(R.id.groupSize); final RadioButton tenPercent = (RadioButton) findViewById(R.id.txtTenPercent); final RadioButton fifteenPercent = (RadioButton) findViewById(R.id.txtFifteenPercent); final RadioButton eighteenPercent = (RadioButton) findViewById(R.id.txtEighteenPercent); final RadioButton twentyPercent = (RadioButton) findViewById(R.id.txtTwentyPercent); final TextView result = (TextView) findViewById(R.id.txtResult); Button convert = (Button) findViewById(R.id.btnConvert); convert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { billTotals = (int) Double.parseDouble(billTotal.getText().toString()); groupsSize = (int) Double.parseDouble(groupSize.getText().toString()); DecimalFormat tenth = new DecimalFormat("#.#"); if (tenPercent.isChecked()) { int percentTotal = (int) ((billTotals * .1) + billTotals); individualTotal = percentTotal / groupsSize; result.setText(tenth.format("$ " + individualTotal)); } else { Toast.makeText(SecondActivity.this, "Please Complete Missing Criteria", Toast.LENGTH_LONG).show(); } if (fifteenPercent.isChecked()) { int percentTotal = (int) ((billTotals * .15) + billTotals); individualTotal = percentTotal / groupsSize; result.setText(tenth.format("$ " + individualTotal)); } else { Toast.makeText(SecondActivity.this, "Please Complete Missing Criteria", Toast.LENGTH_LONG).show(); } if (eighteenPercent.isChecked()) { int percentTotal = (int) ((billTotals * .18) + billTotals); individualTotal = percentTotal / groupsSize; result.setText(tenth.format("$ " + individualTotal)); } else { Toast.makeText(SecondActivity.this, "Please Complete Missing Criteria", Toast.LENGTH_LONG).show(); } if (twentyPercent.isChecked()) { int percentTotal = (int) ((billTotals * .2) + billTotals); individualTotal = percentTotal / groupsSize; result.setText(tenth.format("$ " + individualTotal)); } else { Toast.makeText(SecondActivity.this, "Please Complete Missing Criteria", Toast.LENGTH_LONG).show(); } } }); } }
activity_second.xml
xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txtBill" android:layout_width="200dp" android:layout_height="60dp" android:layout_marginEnd="8dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="@string/txtBill" android:textSize="40sp" app:layout_constraintEnd_toStartOf="@+id/billTotal" app:layout_constraintHorizontal_bias="0.23" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/txtGroupSize" android:layout_width="200dp" android:layout_height="60dp" android:layout_marginEnd="8dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="@string/txtGroupSize" android:textSize="40sp" app:layout_constraintEnd_toStartOf="@+id/groupSize" app:layout_constraintHorizontal_bias="0.229" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtBill" /> <EditText android:id="@+id/billTotal" android:layout_width="209dp" android:layout_height="wrap_content" android:layout_marginEnd="40dp" android:layout_marginTop="16dp" android:ems="10" android:hint="@string/hint" android:inputType="numberDecimal" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/groupSize" android:layout_width="209dp" android:layout_height="wrap_content" android:layout_marginEnd="40dp" android:layout_marginTop="16dp" android:ems="10" android:hint="@string/hint" android:inputType="number" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/billTotal" /> <Spinner android:id="@+id/txtQuality" android:layout_width="519dp" android:layout_height="75dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="28dp" android:entries="@array/txtQuality" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.507" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/groupSize" /> <RadioGroup android:id="@+id/txtTipPercent" android:layout_width="114dp" android:layout_height="211dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtQuality"> <RadioButton android:id="@+id/txtTenPercent" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/tenPercent" android:textSize="25sp" /> <RadioButton android:id="@+id/txtFifteenPercent" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/fifteenPercent" android:textSize="25sp" /> <RadioButton android:id="@+id/txtEighteenPercent" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/eighteenPercent" android:textSize="25sp" /> <RadioButton android:id="@+id/txtTwentyPercent" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/twentyPercent" android:textSize="25sp" /> RadioGroup> <TextView android:id="@+id/txtResult" android:layout_width="317dp" android:layout_height="85dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="25dp" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnConvert" /> <Button android:id="@+id/btnConvert" android:layout_width="276dp" android:layout_height="50dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/btnConvert" android:textSize="25sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtTipPercent" /> android.support.constraint.ConstraintLayout>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started