Need Help With Some Real Estate Math In Java
Solution 1:
Like others have said, "This blows up every time" doesn't give us a lot to go on. That said, I tossed this into a test project and found that it doesn't compile - you're missing a semicolon at the end of this line:
Toast.makeText(jsclosingcost.this, "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT)
By adding a semicolon to that line, I was able to compile this and run it without issue.
A few things of note, however...
You don't need to append an empty string to your strings here:
float fPrice=Float.parseFloat(priceText.getText().toString() + "");
float fRate=Float.parseFloat(rateText.getText().toString() + "");
Appending an empty string in this way is an old trick to ensure things are cast as String objects, but by calling toString on the objects, you've already guaranteed that they're String objects.
I don't know what's considered "best practice", but the first parameter to the Toast.makeText method is a "Context" object. I'd feel more comfortable getting the Context from the view object passed to your onClick handler, like this:
Toast.makeText(v.getContext(), "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT);
You don't have any sorts of checks on your edit fields in case someone fails to fill them in. For example, if someone fails to enter a value into EditText01 and they press your button, you're going to end up with a NullPointerException right here:
float fPrice=Float.parseFloat(priceText.getText().toString() + "");
You could easily guard against this by doing something like this, instead:
publicvoid onClick(View v)
{
Toast.makeText(v.getContext(), "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT);
float fPrice, fRate;
try
{
fPrice = Float.parseFloat(priceText.getText().toString());
fRate = Float.parseFloat(rateText.getText().toString());
float fRealEsate = fPrice * fRate;
Toast.makeText(v.getContext(), "Real Estate Brokerage Fee: "
+ fRealEsate, Toast.LENGTH_SHORT).show();
}
catch (NumberFormatException nfe)
{
Toast.makeText(v.getContext(),
"Please enter numeric values for Price and Rate.",
Toast.LENGTH_SHORT).show();
}
}
Please name your buttons and edittext boxes something better than Button01 and EditText02. Using good names will make your life (and everyone else's lives) easier.
Post a Comment for "Need Help With Some Real Estate Math In Java"