I am making a simple currency calculator for learning purposes and I just have no idea why my code won't work. Currently, when I press the "Convert" button there is a message saying "Appname stopped working"
Here is the code :
public class MainActivity extends AppCompatActivity {
//Button convertButton;
public void convert(View view) {
EditText dkk = (EditText) findViewById(R.id.dkkField);
EditText gbp = (EditText) findViewById(R.id.gbpField);
EditText eur = (EditText) findViewById(R.id.eurField);
EditText usd = (EditText) findViewById(R.id.usdField);
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
TextView textView = (TextView) findViewById(R.id.resultView);
double dkkTogbp;
double dkkToeur;
double dkkTousd;
double gbpToDkk;
double eurToDkk;
double usdToDkk;
if(dkk!=null){
dkkTogbp = dkkNum * 0.114001;
dkkToeur = dkkNum * 0.134489;
dkkTousd = dkkNum * 0.142729;
textView.setText(dkk+ "DKK is "+ dkkTogbp+" GBP, "+dkkToeur+"EUR, "+ dkkTousd +"USD");
}else if(gbp!=null){
gbpToDkk = gbpNum * 8.77189;
textView.setText(dkk+ "GBP is "+ gbpToDkk+" DKK");
}else if(eur!=null){
eurToDkk = eurNum * 7.43555;
textView.setText(dkk+ "EUR is "+ eurToDkk+" DKK");
}else if(usd!=null){
usdToDkk = usdNum * 7.00630;
textView.setText(dkk+ "USD is "+ usdToDkk+" DKK");
Toast.makeText(getApplicationContext(), dkk.toString() + " DKK", Toast.LENGTH_LONG).show();
}
dkk.setText("");
gbp.setText("");
eur.setText("");
usd.setText("");
}
I discovered that when i remove the code block below and the if statements the program can run without crashing :
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
Can someone,please, explain why is this happening?
I don't get any errors.
The problem is by parsing the text to a double value.
At first, you should use a try-catch block to save your application for crashing, like this:
try
{
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
}
catch(NumberFormatException ex)
{
// If the format of the input string is wrong, then you get here the error message
}
catch(NullPointerException ex)
{
// If the input string is null, then you get here the error message
}
Check, whether the text, that should be parsed, has the right syntax, like "1.9".
UPDATE:
If the input string is null, the "parseDouble" method throws a "NullPointerException". For more informations see this link. I have updated the code.
Related
I'm trying to make currency converter app in android studio,but i can't start my app in emulator and i believe it is because some errors in coding.I will copy my code here so you can help me ,I would be grateful.Sorry for my bad English :)
public class MainActivity extends AppCompatActivity {
public void clickFunction(View view) {
Log.i ("Info", "Button pressed");
EditText editText = (EditText) findViewById(editText);
Log.i( "Values" ,editText,getText().toString());
String amountInEuros = editText.getText()
Log.i("Info" , String.valueOf(editText.getText()));
String getAmountInEuros ;
double amountInEuros double = double.parsedouble (amountInEuros);
double amountInDollars double = double amountInEuros double * 1.08 ;
String amountInDollars String = double.toString (amountInDollars double)
Log.i("Amount in Dollars", amountInDollars String);
Toast.makeText ( this , amountInEuros + "Euros" + "is" + amountInDollars String , LENGTH_LONG );
}
These lines are missing a semicolon :
String amountInEuros = editText.getText();
String amountInDollars String = double.toString (amountInDollars double);
I have an EditText field where the user can enter his weight in Kilogram, i.e. 11.7. I want to store that in the database as gramms, i.e. 11700.
How do I convert the String "11.7" to the Integer 11700?
To get the value from an EditText use http://developer.android.com/reference/android/widget/EditText.html#getText()
String userInput = editText.getText().toString();
To convert a String into a double use the code from #govindpatel http://developer.android.com/reference/java/lang/Double.html#parseDouble(java.lang.String)
try {
double kg = Double.parseDouble(userInput)
}
catch (NumberFormatException nfe) {
editText.setError(R.string.invalid_double); // Or whatever you want to show the user
}
To convert from KG to g:
long gramms = Math.round(kg * 1000);
You could replace the double and long with float and int if you know you don't need the precision or range of the larger variables.
you have to parse it to int,long or double.
I think In your case you can do this
double grams = Double.parseDouble("11.7") * 1000;
double variable_name = Double.parseDouble(string_name) * number_you_want_to_multiply_with;
and this will give a double value. So you can convert that to integer, long or keep as it is in your db.
to convert double to long you can do something like this:
long value = (long)(grams);
Hope this helps you,
I'm not sure what it has to do with Android?
int grams = (int)(kg * 1000);
To get text from EditText
EditText mEdit = (EditText)findViewById(R.id.edittext);
double kg = Double.parseDouble(mEdit.getText().toString());
I have two editText. First edit text is amount, second edit text is description. I get the amunt edit text values float, description edit text values string. But error when control values. I think wrong "(tutarEdit.getText().toString().equals("")".
Thanks in Advance..
final EditText tutarEdit = (EditText) layout.findViewById(R.id.editTextTutar);
final EditText aciklamaEdit = (EditText) layout.findViewById(R.id.editTextAciklama);
Float tutar = Float.parseFloat(tutarEdit.getText().toString());
String aciklama = aciklamaEdit.getText().toString();
if(tutarEdit.getText().toString().equals("") || aciklamaEdit.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Void", Toast.LENGTH_LONG).show();
}
You are getting parse error when tutarEdit is "", surround it with try/catch
Float tutar = 0;
try {
tutar = Float.parseFloat(tutarEdit.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
I am trying to ask a user for 2 numbers x and y. When the user clicks the button it will subtract y from x and give the total. What I would like it to do is save the total, and the next time you click the button, it will subtract y from the total. I know I am not passing the total back through again, but I am not sure where to begin.
subtract.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) throws NumberFormatException {
if (v == subtract)
{
NumberFormat currencyFormatter;
currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
String bString;
String x = startingmoney.getText().toString();
String y = submoney.getText().toString ();
double total = 0.00;
double xm = 0.00;
double ym =0.00;
try
{
xm = Double.parseDouble(x);
}
catch(NumberFormatException n)
{
xm = 0.00;
}
try
{
ym = Double.parseDouble(y);
}
catch(NumberFormatException n)
{
ym = 0.00;
}
total = xm -ym;
bString = currencyFormatter.format(total);
endmoney.setText(bString);
tracker.setText("you have entered " + bString +"\n" + tracker.getText().toString());
There are many ways to do this. One possible way is to make the variable "x" and "y" static. This may not meet your requirement, since you may change the value of "x" and "y" dynamically.
Another way is to change the implement of OnClickListener. You can use it as,
class MyOnClickListener implements OnClickListener
and pass the class (the class which calls "setOnClickListener") to MyOnClickListener, that is, the constructor of MyOnClickListener is like,
MyOnClickListener(CLASS clazz){
this.clazz = clazz;
}
And you can do subtraction with clazz.getX() and clazz.getY().
Is there way to convert a string to double. This is how i did.
String s = b.getText().toString();
double d = Double.parseDouble(s);
It gives NumberFormatException
Try this
String s = b.getText().toString();
double d = Double.valueOf(s.trim()).doubleValue();
You are probably starting off with a zero-length string. Check the length before you parse it. It also wouldn't hurt to catch NumberFormatException, although that shouldn't be problem if you have android:inputType="number" in the view's layout.
double d = 0;
CharSequence cs = b.getText();
if(cs != null && cs.length() > 0) {
d = Double.parseDouble(cs.toString());
}
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;