java.lang.NumberFormatException StopWatch.roundTwoDecimals

I am new to Stack Overflow, I'm looking forward to you guys helping me out.
I am new to programming and software development, specifically Android development.

I developed a mobile application and I am receiving this error form the crash reports in the Play Store.

java.lang.NumberFormatException
com.mt.applicaiton.StopWatch.roundTwoDecimals

Could someone please help?

Here is the code:

public class StopWatch extends ActionBarActivity {
    Button butnstart, butnreset,btnstop;
    TextView time;
    long starttime = 0L;
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedtime = 0L;
    int t = 1;
    int secs = 0;
    int mins = 0;
    int milliseconds = 0;
    Handler handler = new Handler();
    SharedPreferences stppreferences;
    SharedPreferences.Editor editor1;
    int stpvalue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stop_watch);
        butnstart = (Button) findViewById(R.id.start);
        butnreset = (Button) findViewById(R.id.reset);
        btnstop = (Button) findViewById(R.id.stop);
        time = (TextView) findViewById(R.id.timer);

        stppreferences = PreferenceManager.getDefaultSharedPreferences(StopWatch.this);


        butnstart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                if (t == 1) {
                    butnstart.setText("Pause");
                    starttime = SystemClock.uptimeMillis();
                    handler.postDelayed(updateTimer, 0);
                    t = 0;
                } else {
                    butnstart.setText("Start");
                    time.setTextColor(Color.BLUE);
                    timeSwapBuff += timeInMilliseconds;
                    handler.removeCallbacks(updateTimer);
                    t = 1;
               }

            }
        });

        btnstop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    String text = time.getText().toString();
                    String[] timelist = text.split(":");
                    String strmin = timelist[0];
                    String strsecond = timelist[1];
                    String strmilisecond = timelist[2];
                    double min =Double.parseDouble(strmin);
                    double fmin= min * 60;
                    Float mili = Float.parseFloat(strmilisecond);
                    double convermili= mili/1000.0;

                    double result= fmin+ Double.parseDouble(strsecond)+convermili ;
                    double myresult= roundTwoDecimals(result);
                    String strresult= Double.toString(myresult);


                  // Setting name of button in collect sample
                SharedPreferences settings = getSharedPreferences("watchdynamicbtname", 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("watchbtnname",strresult);
                editor.commit();
                // Getting value from CollectSample Click for setting particular name
                stppreferences = PreferenceManager.getDefaultSharedPreferences(StopWatch.this);
                stpvalue= stppreferences.getInt("stpvalue",0);
                int x= stpvalue;
               // calling particular button name CollectSample.OnResume
                editor1 = PreferenceManager.getDefaultSharedPreferences(StopWatch.this).edit();
                editor1.putInt("stpvalue",x).apply();
                starttime = SystemClock.uptimeMillis();
                handler.postDelayed(updateTimer, 0);
                handler.removeCallbacks(updateTimer);
                finish();

            }
        });




        butnreset.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                starttime = 0L;
                timeInMilliseconds = 0L;
                timeSwapBuff = 0L;
                updatedtime = 0L;
                t = 1;
                secs = 0;
                mins = 0;
                milliseconds = 0;
                butnstart.setText("Start");
                handler.removeCallbacks(updateTimer);
                time.setText("00:00:00");

            }
        });

    }

    public Runnable updateTimer = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - starttime;

            updatedtime = timeSwapBuff + timeInMilliseconds;

            secs = (int) (updatedtime / 1000);
            mins = secs / 60;
            secs = secs % 60;
            milliseconds = (int) (updatedtime % 1000);
            time.setText("" + mins + ":" + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            time.setTextColor(Color.RED);
            handler.postDelayed(this, 0);
        }

    };
    roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
}
}

And here is the Logcat:

java.lang.NumberFormatException: 
  at java.lang.StringToReal.invalidReal (StringToReal.java:63)
  at java.lang.StringToReal.initialParse (StringToReal.java:164)
  at java.lang.StringToReal.parseDouble (StringToReal.java:282)
  at java.lang.Double.parseDouble (Double.java:301)
  at java.lang.Double.valueOf (Double.java:338)
  at com.mt.puzzlesolutions.free.version.StopWatch.roundTwoDecimals (StopWatch.java:168)
  at com.mt.puzzlesolutions.free.version.StopWatch$2.onClick (StopWatch.java:86)
  at android.view.View.performClick (View.java:5207)
  at android.view.View$PerformClick.run (View.java:21177)
  at android.os.Handler.handleCallback (Handler.java:739)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:5458)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:738)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:628)
Jon Skeet
people
quotationmark

Double.valueOf expects a value formatted with a dot as a decimal point, always. You're creating a DecimalFormat that uses the system default locale, and the . there is the locale-specific decimal separator.

The smallest change that might work would be to use:

DecimalFormat twoDForm = new DecimalFormat(
    "#.##", DecimalFormatSymbols.getInstance(Locale.US));

That will always use the US symbols, which have a dot as a decimal separator.

However, you should expect that you still won't have a double value which is exactly two decimal places. For example, 0.01 has no precise double representation, so you'll end up with "the nearest double to 0.01".

Given that when you call roundTwoDecimals you then immediately format the string, you might want to just change it to return a string (and rename it accordingly):

String formatTo2DP(double d) {
    DecimalFormat twoDForm = new DecimalFormat(
        "#.##", DecimalFormatSymbols.getInstance(Locale.US));
    return twoDForm.format(d);
}

There's no point in formatting, then parsing, then formatting again.

Then change this calling code:

double myresult= roundTwoDecimals(result);
String strresult= Double.toString(myresult);

to just:

String formattedResult = formatTo2DP(result);

people

See more on this question at Stackoverflow