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)









share|improve this question



























    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)









    share|improve this question

























      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)









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 23:56

























      asked Nov 10 at 20:29









      Black Blouse

      12




      12



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党