Decimal Format Method Crash
up vote
0
down vote
favorite
I'm building an app that receives the input from EditText widgets, calculates and displays the results. I have built the xml draft but I am a begginer so I am a bit stuck on java...
Specifically I want help on making the resulted number appear rounded to 2 decimals.
When I enter the Decimal Format method, once I tap the "Calculate" Button, the app crashes.
Here's the code I wrote till now.
IMAGE of what the app looks like
package com.example.android.deyalcalculator;
EditText mEditX1a;
EditText mEditX1b;
EditText mEditX1c;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalculateButton = (Button) findViewById(R.id.calculateButton);
mEditX1a = (EditText) findViewById(R.id.aTextX1);
mEditX1b = (EditText) findViewById(R.id.bTextX1);
mEditX1c = (EditText) findViewById(R.id.cTextX1);
CalculateButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String X1a = mEditX1a.getText().toString();
double X1aInput = Double.parseDouble(X1a);
String X1b = mEditX1b.getText().toString();
double X1bInput = Double.parseDouble(X1b);
String X1c = mEditX1c.getText().toString();
double X1cInput = Double.parseDouble(X1c);
Double dX1Result = ((X1bInput - X1aInput) / 25) * 1000;
TextView dX1TextView = findViewById(R.id.dX1);
String dX1StringDouble = Double.toString(dX1Result);
dX1TextView.setText(dX1StringDouble);
new DecimalFormat ("#.00").format(dX1StringDouble); //(Without this it works)
);
}
The Error is this:
Process: com.example.android.deyalcalculator, PID: 12243
java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.android.deyalcalculator.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6354)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
methods crash rounding display decimalformat
add a comment |
up vote
0
down vote
favorite
I'm building an app that receives the input from EditText widgets, calculates and displays the results. I have built the xml draft but I am a begginer so I am a bit stuck on java...
Specifically I want help on making the resulted number appear rounded to 2 decimals.
When I enter the Decimal Format method, once I tap the "Calculate" Button, the app crashes.
Here's the code I wrote till now.
IMAGE of what the app looks like
package com.example.android.deyalcalculator;
EditText mEditX1a;
EditText mEditX1b;
EditText mEditX1c;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalculateButton = (Button) findViewById(R.id.calculateButton);
mEditX1a = (EditText) findViewById(R.id.aTextX1);
mEditX1b = (EditText) findViewById(R.id.bTextX1);
mEditX1c = (EditText) findViewById(R.id.cTextX1);
CalculateButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String X1a = mEditX1a.getText().toString();
double X1aInput = Double.parseDouble(X1a);
String X1b = mEditX1b.getText().toString();
double X1bInput = Double.parseDouble(X1b);
String X1c = mEditX1c.getText().toString();
double X1cInput = Double.parseDouble(X1c);
Double dX1Result = ((X1bInput - X1aInput) / 25) * 1000;
TextView dX1TextView = findViewById(R.id.dX1);
String dX1StringDouble = Double.toString(dX1Result);
dX1TextView.setText(dX1StringDouble);
new DecimalFormat ("#.00").format(dX1StringDouble); //(Without this it works)
);
}
The Error is this:
Process: com.example.android.deyalcalculator, PID: 12243
java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.android.deyalcalculator.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6354)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
methods crash rounding display decimalformat
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm building an app that receives the input from EditText widgets, calculates and displays the results. I have built the xml draft but I am a begginer so I am a bit stuck on java...
Specifically I want help on making the resulted number appear rounded to 2 decimals.
When I enter the Decimal Format method, once I tap the "Calculate" Button, the app crashes.
Here's the code I wrote till now.
IMAGE of what the app looks like
package com.example.android.deyalcalculator;
EditText mEditX1a;
EditText mEditX1b;
EditText mEditX1c;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalculateButton = (Button) findViewById(R.id.calculateButton);
mEditX1a = (EditText) findViewById(R.id.aTextX1);
mEditX1b = (EditText) findViewById(R.id.bTextX1);
mEditX1c = (EditText) findViewById(R.id.cTextX1);
CalculateButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String X1a = mEditX1a.getText().toString();
double X1aInput = Double.parseDouble(X1a);
String X1b = mEditX1b.getText().toString();
double X1bInput = Double.parseDouble(X1b);
String X1c = mEditX1c.getText().toString();
double X1cInput = Double.parseDouble(X1c);
Double dX1Result = ((X1bInput - X1aInput) / 25) * 1000;
TextView dX1TextView = findViewById(R.id.dX1);
String dX1StringDouble = Double.toString(dX1Result);
dX1TextView.setText(dX1StringDouble);
new DecimalFormat ("#.00").format(dX1StringDouble); //(Without this it works)
);
}
The Error is this:
Process: com.example.android.deyalcalculator, PID: 12243
java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.android.deyalcalculator.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6354)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
methods crash rounding display decimalformat
I'm building an app that receives the input from EditText widgets, calculates and displays the results. I have built the xml draft but I am a begginer so I am a bit stuck on java...
Specifically I want help on making the resulted number appear rounded to 2 decimals.
When I enter the Decimal Format method, once I tap the "Calculate" Button, the app crashes.
Here's the code I wrote till now.
IMAGE of what the app looks like
package com.example.android.deyalcalculator;
EditText mEditX1a;
EditText mEditX1b;
EditText mEditX1c;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalculateButton = (Button) findViewById(R.id.calculateButton);
mEditX1a = (EditText) findViewById(R.id.aTextX1);
mEditX1b = (EditText) findViewById(R.id.bTextX1);
mEditX1c = (EditText) findViewById(R.id.cTextX1);
CalculateButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String X1a = mEditX1a.getText().toString();
double X1aInput = Double.parseDouble(X1a);
String X1b = mEditX1b.getText().toString();
double X1bInput = Double.parseDouble(X1b);
String X1c = mEditX1c.getText().toString();
double X1cInput = Double.parseDouble(X1c);
Double dX1Result = ((X1bInput - X1aInput) / 25) * 1000;
TextView dX1TextView = findViewById(R.id.dX1);
String dX1StringDouble = Double.toString(dX1Result);
dX1TextView.setText(dX1StringDouble);
new DecimalFormat ("#.00").format(dX1StringDouble); //(Without this it works)
);
}
The Error is this:
Process: com.example.android.deyalcalculator, PID: 12243
java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.android.deyalcalculator.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6354)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
methods crash rounding display decimalformat
methods crash rounding display decimalformat
edited Nov 10 at 23:56
asked Nov 10 at 20:29
Black Blouse
12
12
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243126%2fdecimal-format-method-crash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown