0

I am having some issues with this application. Everything looks fine to me, but when I run the app it loads perfectly, till I click the calculate button and try to make it think. Please give me a hint on what to fix or where to start looking because I am stumped!

Problem:
1. The opening screen requests the type of car wash package you would like to purchase.

  1. The user selects which type of car wash — exterior only or exterior with interior vacuum services. The Car Wash app charges $8.99 for an exterior wash and $12.99 for an exterior wash with an interior vacuum for a package of 12 or more car washes. If you select less than 12 washes, the charge is $10.99 for an exterior wash or $15.99 for an exterior with interior vacuum.

  2. When the CALCULATE PACKAGE button is selected, the total price is displayed for the number of car washes purchased (Figure 4-36).

Error received:

 E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: net.androidbootcamp.carwashapp, PID: 3212
                      java.lang.IllegalArgumentException: Cannot format given Object as a Number
                          at java.text.DecimalFormat.format(DecimalFormat.java:593)
                          at java.text.Format.format(Format.java:157)
                          at net.androidbootcamp.carwashapp.MainActivity$1.onClick(MainActivity.java:53)
                          at android.view.View.performClick(View.java:5610)
                          at android.view.View$PerformClick.run(View.java:22265)
                          at android.os.Handler.handleCallback(Handler.java:751)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Java Code:

package net.androidbootcamp.carwashapp;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
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 com.google.android.gms.common.api.GoogleApiClient;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    double priceOne = 10.99;
    double discountOne = 8.99;
    double priceTwo = 15.99;
    double discountTwo = 12.99;
    double total;
    double washesEntered;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        final EditText numWashes = (EditText) findViewById(R.id.numOfWashes);
        final RadioButton exteriorOnly = (RadioButton) findViewById(R.id.radExteriorOnly);
        final RadioButton exteriorInterior = (RadioButton) findViewById(R.id.radExteriorInterior);
        final TextView result = (TextView) findViewById(R.id.txtResult);
        Button calc = (Button) findViewById(R.id.btnCalulate);

        calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                washesEntered = Double.parseDouble(numWashes.getText().toString());
                DecimalFormat tenth = new DecimalFormat("#.#");
                if (exteriorOnly.isChecked() && washesEntered < 12) {
                    total = washesEntered * priceOne;
                    result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                }
                if (exteriorOnly.isChecked() && washesEntered > 12) {
                    total = washesEntered * discountOne;
                    result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                }
                if (exteriorInterior.isChecked() && washesEntered < 12) {
                    total = washesEntered * priceTwo;
                    result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                }
                if (exteriorInterior.isChecked() && washesEntered > 12) {
                    total = washesEntered * discountTwo;
                    result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));
                }

            }
        });


    }


}

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    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="net.androidbootcamp.carwashapp.MainActivity">

    <TextView
        android:text="@string/txtDiscount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtDiscount"
        android:textSize="18sp"
        android:layout_below="@+id/txtTitle"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="@string/btnCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnCalulate"
        android:textSize="24sp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />


    <TextView
        android:text="@string/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtTitle"
        android:textSize="36sp"
        android:textColor="#ffa500"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/carwash"
            android:id="@+id/imgCarWash"
            android:contentDescription="@string/imgDescription"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtResult"
            android:layout_marginTop="46dp"
            android:layout_alignTop="@+id/imgCarWash"
            android:layout_alignEnd="@+id/txtTitle"
            android:textSize="18sp"
            android:textColor="#0000ff"
            android:textAlignment="center" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="18dp"
        android:layout_below="@+id/txtDiscount"
        android:layout_alignStart="@+id/txtDiscount">

        <RadioButton
            android:text="@string/radExteriorInterior"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radExteriorOnly"
            android:layout_alignStart="@+id/radExteriorOnly"
            android:layout_marginTop="22dp"
            android:id="@+id/radExteriorInterior" />


        <RadioButton
        android:text="@string/radExteriorOnly"
        android:layout_width="178dp"
        android:layout_height="wrap_content"
        android:id="@+id/radExteriorOnly"
        android:layout_below="@+id/numOfWashes"
        android:layout_toEndOf="@+id/radioGroup4"
        android:layout_marginStart="13dp" />
    </RadioGroup>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/numOfWashes"
        android:hint="@string/numOfWashes"
        android:layout_below="@+id/txtDiscount"
        android:layout_alignEnd="@+id/txtTitle"
        android:layout_alignStart="@+id/btnCalulate" />

</RelativeLayout>
nqsherman
  • 13
  • 2

1 Answers1

0

Your use of DecimalFormat is incorrect; you basically have:

DecimalFormat tenth = new DecimalFormat("#.#");
result.setText(tenth.format("$" + total + " for " + washesEntered + " washes"));

DecimalFormat works only to format a single number not a whole line of output, so something like this:

result.setText("$" + tenth.format(total) + " for " + washesEntered + " washes"));

Should produce a better result.

(Note also my comment regarding testing what happens with washesEntered of 12, 0.5 and -1.)

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • I added the error into my original post. I apologize, this is my first time using Stackoverflow – nqsherman Dec 19 '16 at 02:13
  • it looks as thought it is because of the decimal format like you said, which was just my ignorance not seeing that right off the bat – nqsherman Dec 19 '16 at 02:15
  • 1
    The reason we ask for your LogCat or stack trace is that it helps not just us but also you; searching Google for `java.lang.IllegalArgumentException: Cannot format given Object as a Number` gives an answer to your problem which you can either use yourself to solve the problem on your own, or you can use that to narrow down your question if there is something about the answer you cannot follow. This is a win-win - you get a precise answer, and we don't waste time trying to find the real root of the issue. Oh, and Welcome to StackOverflow! – Ken Y-N Dec 19 '16 at 02:20