-2

Here is my code first:

public class MainActivity extends AppCompatActivity
{
Spinner dropdown;
EditText e1;  //user enters the bill amount before tip here
TextView tipped; //total tipped amount
TextView Bill;  //the total amount of the bill including tax
String sdropdown;
double originalBill = 0;  //amount that the user enters converted to a double
double result = 0;  //result of multiplying bill and tax
String test1;  //editText field has to be converted to a string value first
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();  //currency format object



//Create array of items
String tip [] = {"10%", "15%", "20%", "25%", "30%"};

//Create ArrayAdaptor
ArrayAdapter<String> adaptorTipAmount;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Create the element
    dropdown = (Spinner) findViewById(R.id.tipAmount);
    tipped = (TextView) findViewById(R.id.tipTotal);
    Bill = (TextView) findViewById(R.id.billTotal);
    e1 = (EditText) findViewById(R.id.billAmount);
    tipped.setText(currencyFormat.format(0));
    Bill.setText(currencyFormat.format(0));


    //initialize and set adaptor
    adaptorTipAmount = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, tip);

    adaptorTipAmount.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dropdown.setAdapter(adaptorTipAmount);



    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapter, View v,
                                   int position, long id) {

            // On selecting a spinner item
            sdropdown = adapter.getItemAtPosition(position).toString();

            //what editText field is initially stored in
            test1 = e1.getText().toString();

            //test1 string variable gets converted to a double
            //originalBill = Double.parseDouble(test1);



            //determines the tip amount and does the calculation based on the tip
           /* if (sdropdown.equals("10%"))
            {
                result = originalBill * .10;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("15%"))
            {
                result = originalBill * .15;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("20%"))
            {
                result = originalBill * .20;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("25%"))
            {
                result = originalBill * .25;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("30%"))
            {
                result = originalBill * .30;
                tipped.setText(Double.toString(result));
            }
            */
            tipped.setText("The tipped amount is: " + test1);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub
        }
    });
}
}

The issue I'm having is whenever I try to get the value from the EditText field and store it in a double the app crashes. I've searched around for solutions to this and have found that if you store the field in a string first and then convert it to a double that would work....well it did not. I'm pretty certain that this is my error although i'm out of ideas on how to solve. I don't want to use a button either. Any help? Thanks

Ryan
  • 29
  • 1
  • 8
  • Can you be a little more specific? Where exactly does it crash and what is the exception (if any).In general, I can see that you do a lot of findViewBy Id without validating the result. This is asking for trouble. Wrapping the code into try/catch block and checking for exceptions also helps a lot. – Victor Havin Sep 16 '16 at 03:50
  • It's crashes right where originalBill = parsedouble.... – Ryan Sep 16 '16 at 03:56
  • If I comment that out and just try to output test1 which is a string it will work but trying to output originalBill won't work – Ryan Sep 16 '16 at 03:57
  • What exception do you get? If you get NumberFormatException, then your string is not properly formatted. BTW, what is in the string you are trying to parse? If you get NullPointerException - you did not succeed in retrieving the string from your form. – Victor Havin Sep 16 '16 at 04:05
  • I'm taking the test1 variable and getting the value of the EditText field e1. To test this I do a setText and output the value of test1 which works. Then, I'm attempting to parse test1 to a double and store it in originalBill variable which fails. I ushually have this commented out but in my above code I dident. – Ryan Sep 16 '16 at 04:16
  • You did not answer my question: What exception did you get (there are at least two possible)? What is the the value of the test1 string when you use it in the parseDouble method? – Victor Havin Sep 16 '16 at 04:30
  • http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Fletcher Johns Sep 16 '16 at 06:05
  • the value of the test1 string is 12.50. I'm not sure but can check on the exception that im getting – Ryan Sep 16 '16 at 13:34

3 Answers3

0

Try this:

How about use toString() at the end of:

tipped.setText(currencyFormat.format(0)).toString();

Hope it Helps.

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
0

First off you need to understand the difference between the two types. double is a primitive type whereas Double is an Object. In your global variable: Double originalBill;

//what editText field is initially stored in
test1 = String.valueOf(e1.getText().toString());  //clear any chance if no string in editText

//test1 string variable gets converted to a double
if(test1 != null)
originalBill = Double.parseDouble(test1);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

You may need to clean up the String you get from the EditText. Use trim() to remove any whitespace, if any.

        //what editText field is initially stored in
        test1 = e1.getText().toString().trim();

Have a look at what value is stored in the String

        Log.v("parseDouble", "test1 = " + test1);

Ensure it isn't empty, if it is make it "0"

        if (test1.isEmpty()) test1 = "0";
        //test1 string variable gets converted to a double
        originalBill = Double.parseDouble(test1);
Fletcher Johns
  • 1,236
  • 15
  • 20