WiMa WiMa - 1 month ago 9
Android Question

My app crashes when my phone's language is set to Persian or Arabic because of Double.parseDouble

below is the java code for one of my activities, it works fine when the phone's language is English but when the phone's language is set to Persian or Arabic it crashes. the logcat says that it is something wrong with the Double.parseDouble. it seems when the user enters the numbers it changes them into Persian or Arabic digits and thats why it crashes. Any ideas how to fix?

The java code:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class stressesoninclinedsections extends AppCompatActivity {

private EditText input1;
private EditText input2;
private EditText input3;

private TextView sh_resultt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stressesoninclinedsections);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

input1 = (EditText) findViewById(R.id.editText);
input2 = (EditText) findViewById(R.id.editText2);
input3 = (EditText) findViewById(R.id.editText3);

Button bt_calculate1 = (Button) findViewById(R.id.button4);

sh_resultt = (TextView) findViewById(R.id.textView8);

bt_calculate1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeCalculationss();
}
});
}

private void makeCalculationss() {
if ( input1.getText().toString().trim().equals("") || input1.getText().toString().trim().equals("-") || input1.getText().toString().trim().equals(".") || input1.getText().toString().trim().equals("-.")
|| input2.getText().toString().trim().equals("") || input2.getText().toString().trim().equals(".")
|| input3.getText().toString().trim().equals("") || input3.getText().toString().trim().equals("-") || input3.getText().toString().trim().equals(".") || input3.getText().toString().trim().equals("-.")) {
sh_resultt.setText("");
Snackbar.make(findViewById(android.R.id.content), "لطفا مقادیر تمامی متغیرها را وارد کنید ", Snackbar.LENGTH_LONG)
.show();
}
else {
Double n1 = Double.valueOf(input1.getText().toString());
Double n2 = Double.valueOf(input2.getText().toString());
Double n3 = Double.valueOf(input3.getText().toString());
Double n3pi2 = n3+90;
Double n3pi2rad = (n3pi2*Math.PI)/180;
Double n3radians = (n3*Math.PI)/180;
Double result1 = ((n1 / n2) *Math.cos(n3radians)*Math.cos(n3radians))/1000;
Double result2 = ((n1 / n2) *Math.cos(n3pi2rad)*Math.cos(n3pi2rad))/1000;
Double result3 = -((n1 / n2) *Math.sin(n3radians)*Math.cos(n3radians))/1000;
result1 =Double.parseDouble(new DecimalFormat("##.######").format(result1));
result2 =Double.parseDouble(new DecimalFormat("##.######").format(result2));
result3 =Double.parseDouble(new DecimalFormat("##.######").format(result3));
sh_resultt.setText( "σx' (MPa)=\n" + result1 + "\n" + "\nσy' (MPa)=\n" + result2 + "\n" + "\nTx'y' (MPa)=\n" + result3 );
}
}


@Override
public void onBackPressed() {
Intent intent = new Intent(stressesoninclinedsections.this, solidmechanics.class);
startActivity(intent);
finish();
}

}


Here is the Logcat:

03-08 19:16:32.457 3035-3035/com.wima.civilengineeringcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid long: "٠٫٠١١٧٥٢"
at java.lang.Long.invalidLong(Long.java:125)
at java.lang.Long.parse(Long.java:362)
at java.lang.Long.parseLong(Long.java:353)
at java.lang.Long.parseLong(Long.java:319)
at java.math.BigDecimal.<init>(BigDecimal.java:350)
at java.math.BigDecimal.<init>(BigDecimal.java:438)
at com.wima.civilengineeringcalculator.stressesoninclinedsections.makeCalculationss(stressesoninclinedsections.java:65)
at com.wima.civilengineeringcalculator.stressesoninclinedsections.access$000(stressesoninclinedsections.java:16)
at com.wima.civilengineeringcalculator.stressesoninclinedsections$1.onClick(stressesoninclinedsections.java:42)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14393)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
03-08 19:16:32.467 1595-2005/? E/EmbeddedLogger: App crashed! Process: com.wima.civilengineeringcalculator
03-08 19:16:32.467 1595-2005/? E/EmbeddedLogger: App crashed! Package: com.wima.civilengineeringcalculator v1 (1.0)

Answer

Thanks to all who answered specially Alaa M., because I somehow found the answer through his comment. The "," in Persian or Arabic digits that we use to write the decimals with it made the problem.

I managed to fix it by adding

Locale.setDefault(new Locale("en", "US"));

to my code.