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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:54:20+00:00 2026-06-05T01:54:20+00:00

Can anyone take a look at the below code and point out the obvious

  • 0

Can anyone take a look at the below code and point out the obvious on what’s wrong? The program gives an error when editText is left blank and only does the calculation once all the information is present. Currently the program crashes when there is text missing, why is this?

EditText editText1, editText2;
double numA, numB, numC;
TextView answer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calc);

    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
}

public void btnClick(View v)
{
    if(editText1.getText().length()==0)
    {
        editText1.setError("please input text"); 
    }{
        if  (editText2.getText().length()==0)
            {
                editText2.setError("please input text");
            }

    numA = new Double(editText1.getText().toString());
    numB = new Double(editText2.getText().toString());

    numC=(numA + numB); answer.setText(Double.toString(numC));
    }
}}
  • 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-05T01:54:22+00:00Added an answer on June 5, 2026 at 1:54 am

    The problem is that you’re trying to parse "" into a number (a double, specifically). You are checking for empty values, but you’re still attempting to parse it even if you set the error. You need to place the new Double() statements in a try-catch block, and catch the exceptions that happen when trying to parse an invalid input. This way, it’s safe even if the user inputs a random string of text that is also not parseable as a double.

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

Sidebar

Related Questions

Can anyone show a short and simple code snippet? It should take the name
Can anyone tell the block animation equivalent of this below code snippet? [UIView beginAnimations:@View
Probably I completely don't understand it, so can you take a look at code
Please take a look at this link: http://bethhaim.spin-demo.com/ I can't figure out why there
So take a look at the below rough code: function Manager() { try {
Very simple question. Take a look at the code below. It's a very simple
Please take a look at the code snippet below: interface IFoo<E>{ void doFoo(E env);
I've now added a bounty which will be awarded to anyone who can take
I am stuck on this ClassCastException error for a while now, can anyone help
Can anyone conjure from this code why the ItemsSource line would be getting a

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.