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 7853329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:39:04+00:00 2026-06-02T19:39:04+00:00

In my project I have a button that calculate at at least 2 EditBox,

  • 0

In my project I have a button that calculate at at least 2 EditBox, but when I leave it empty and calculate I got crash app:

java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:248)
at java.lang.Double.parseDouble(Double.java:295)
at com.math.square.Level1.calculate(Level1.java:71)
at com.math.square.Level1.access$0(Level1.java:66)
at com.math.square.Level1$2.onClick(Level1.java:62)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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)

What can I do to check before calculate, and if it’s empty, alert the User and do not calculate?

The code:

public class Level1 extends Activity {
    EditText amount1;
    EditText amount2;
    EditText amount3;
    EditText amount4;
    Button calculate;
    double a=0;
    double b=0;
    double c=0;
    double d=0;
    double e=0;
    double f=0;
    double g=0;
    double h=0;

    /** Called when the activity is first created. */
    @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.level1);

          initControls();

        findViewById(R.id.Button05).setOnClickListener(
                new OnClickListener() {
            public void onClick(View v) {
                btn05(true);
            }
        });;
      }

          private void initControls()
          {
              amount1=(EditText)findViewById(R.id.amount1);
              amount2=(EditText)findViewById(R.id.amount2);
              amount3=(EditText)findViewById(R.id.amount3);
              amount4=(EditText)findViewById(R.id.amount4);
              calculate=(Button)findViewById(R.id.calculate);
              calculate.setOnClickListener(new Button.OnClickListener()
              {
                  public void onClick
              (View  v) { calculate();}
              });
          }

        private void calculate()
          {
              a=Double.parseDouble(amount1.getText().toString());
              b=Double.parseDouble(amount2.getText().toString());
              d=Double.parseDouble(amount3.getText().toString());
              e=Double.parseDouble(amount4.getText().toString());

              /** Check Value > 0*/
              EditText amount1value = (EditText)this.findViewById(R.id.amount1);
              EditText amount2value = (EditText)this.findViewById(R.id.amount2);
              EditText amount3value = (EditText)this.findViewById(R.id.amount3);
              EditText amount4value = (EditText)this.findViewById(R.id.amount4);
              Integer nameLen = 2;

              int len = amount1value.length();
              int len2 = amount2value.length();
              int len3 = amount3value.length();
              int len4 = amount4value.length();

              if (len<nameLen) {
                  amount1value.setError(getText(R.string.blank));
                  TextView Result = (TextView)findViewById(R.id.Result);
                  Result.setText(R.string.error_character_length);
              } else 
              if (len2<nameLen) {
                  amount2value.setError(getText(R.string.blank));
                  TextView Result = (TextView)findViewById(R.id.Result);
                  Result.setText(R.string.error_character_length);
              } else 
              if (len3<nameLen) {
                  amount3value.setError(getText(R.string.blank));
                  TextView Result = (TextView)findViewById(R.id.Result);
                  Result.setText(R.string.error_character_length);
              } else 
              if (len4<nameLen) {
                  amount4value.setError(getText(R.string.blank));
                  TextView Result = (TextView)findViewById(R.id.Result);
                  Result.setText(R.string.error_character_length);
              }  

              else {

              c=a+b;
              f=d+e;
              g=a+d;
              h=b+e;

              if(c==9 && f==12 && g==8 && h==13){
                TextView Result = (TextView)findViewById(R.id.Result);
                Result.setText(R.string.correct);
              }
              else {
                TextView Result = (TextView)findViewById(R.id.Result);
                Result.setText(R.string.wrong);
              }
              }
          }

I created a check to see if there are 2 chars to try to avoid blank for users, but it’s just a workaround.

  • 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-02T19:39:06+00:00Added an answer on June 2, 2026 at 7:39 pm

    The answer is way simpler. Just check the EditText before for text before you run the calculation. If its empty, display a error toast. Otherwisee continue with the calculation. Another thing to note is your EditText should only accept numbers as the input type. No need for a try catch. For details look up the EditText documentation on the dev site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Delphi Project i want to have a 'Settings' button that when clicked,
I have a simple wpf app which has a button that increments a value
I have a Cocoa app that does a number of things, but among them,
In my project I have a button that calls a function via a IBOutlet,
i have a div in my project and on this div a button that
In my project i have a button. when user clicks on it, it shows
I have project that uses Hibernate: hibernate-core-3.6.7.Final.jar In its POM I found: <dependency> <groupId>org.hibernate.javax.persistence</groupId>
I have a C++ project that have like 15+ external libraries installed with a
I have two sections in a form, and I have a button that toggles
I have a project for a client that will be using a YouTube comment

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.