Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:08:24+00:00 2026-06-04T20:08:24+00:00

I am creating an app which divides one number by another number. I have

  • 0

I am creating an app which divides one number by another number. I have tried to add code to display a dialog when there is an empty edit text, but the app forcecloses. Here is my code.

public class KdCalculator extends Activity implements OnClickListener{

    EditText kills;
    EditText deaths;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kd_activity);

        Button calculate = (Button)findViewById(R.id.ButtonCalculate);
            calculate.setOnClickListener(this);

        kills = (EditText)findViewById(R.id.EditTextKills);
        deaths = (EditText)findViewById(R.id.EditTextDeaths);

    }

    public void onClick(View paramView)
      { 
          boolean invalid = false;

          String str1 = kills.getText().toString();
          String str2 = deaths.getText().toString();

          if (str1.equals("") || str2.equals(""))
          {
              new AlertDialog.Builder(this)
                .setTitle("No Number")
                .setMessage("Please enter a number for both kills and deaths!")
                .setNeutralButton("Ok", null)
                .show();
              invalid = true;
          }

          double d1 = Double.parseDouble(str2);
          double d2 = Double.parseDouble(str1);

          if ((d1 == 0.0D) || (d2 == 0.0D))
          {
              new AlertDialog.Builder(this)
            .setTitle("Invalid Number")
            .setMessage("Please enter a number different than 0 for both kills and deaths!")
            .setNeutralButton("Ok", null)
            .show();
              invalid = true;
          }
          double d3 = d2 / d1;
          DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
          String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);

          if(!invalid){
          new AlertDialog.Builder(this)
            .setTitle("K/D Ratio")
            .setMessage(str3)
            .setNeutralButton("Ok", null)
            .show();
          }
          invalid = false;
        }


}

the logcat error is:

05-27 11:43:53.327: W/dalvikvm(6090): threadid=1: thread exiting with uncaught exception (group=0x40020b80)
05-27 11:43:53.357: E/AndroidRuntime(6090): FATAL EXCEPTION: main
05-27 11:43:53.357: E/AndroidRuntime(6090): java.lang.NumberFormatException: 
05-27 11:43:53.357: E/AndroidRuntime(6090):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.Double.parseDouble(Double.java:287)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.blackops2.KdCalculator.onClick(KdCalculator.java:53)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.view.View.performClick(View.java:2411)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.view.View$PerformClick.run(View.java:8819)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Handler.handleCallback(Handler.java:587)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.os.Looper.loop(Looper.java:123)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at java.lang.reflect.Method.invoke(Method.java:521)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-27 11:43:53.357: E/AndroidRuntime(6090):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:08:25+00:00Added an answer on June 4, 2026 at 8:08 pm

    Even if your EditText are empty you’re still parsing the empty Strings from those EditText as your code continues to be executed after you show the AlertDialog, instead you should return from the onClick method to stop any calculations(when the EditText are empty or aren’t filled):

    public void onClick(View paramView) { 
              String str1 = kills.getText().toString();
              String str2 = deaths.getText().toString();
    
              if (str1.equals("") || str2.equals("")) {
                  new AlertDialog.Builder(this)
                    .setTitle("No Number")
                    .setMessage("Please enter a number for both kills and deaths!")
                    .setNeutralButton("Ok", null)
                    .show();
                    return;   
              }
              double d1 = Double.parseDouble(str2);
              double d2 = Double.parseDouble(str1);
              if ((d1 == 0.0D) || (d2 == 0.0D)) {
                  new AlertDialog.Builder(this)
                .setTitle("Invalid Number")
                .setMessage("Please enter a number different than 0 for both kills and deaths!")
                .setNeutralButton("Ok", null)
                .show();
                return;
              }
              double d3 = d2 / d1;
              DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
              String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);
    
              new AlertDialog.Builder(this)
                .setTitle("K/D Ratio")
                .setMessage(str3)
                .setNeutralButton("Ok", null)
                .show();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an app which will have a question in a UILabel and
I'm creating a Django app in which I have a table which has 'items',
I'm creating a Facebook app in which I want to display content depending on
I am creating an iPhone app which I would like to have a similar
I have started creating an app which uses a core data stack at the
I am creating an app which uses Core Data. I have a flip-side in
i'm creating an app in which i have a uiwebview. I want to load
I'm creating an iPhone app which is going to have an online spreadsheet to
I am creating a EventsManager app in which i have a BroadcastReciver which executes
I'm creating an app which is going to have some data that is stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.