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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:44:00+00:00 2026-06-17T22:44:00+00:00

I am trying to learn java while building an android app. I have a

  • 0

I am trying to learn java while building an android app. I have a points calculator without a button it uses a textchange listener to calculate the total. When backspace key is pressed and the box has null it crashes. I tried validating using the code below (only validated on field to begin with). But it does not work. Any help would be greatly appreciated.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wwcalc);
    etFat = (EditText)findViewById(R.id.editTextFat);
    etFiber = (EditText)findViewById(R.id.editTextFiber);
    etProtein = (EditText)findViewById(R.id.editTextProtein);
    etCarbs = (EditText)findViewById(R.id.editTextCarbs);
    tvTotal = (TextView)findViewById(R.id.textViewPoints);

    TextWatcher watcher = new TextWatcher() {

        @Override 
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if(isEmpty(etFat) == false){
                intFat = Integer.parseInt(etFat.getText().toString());
            }
            else{

                etFat.setText("0");
                etFat.hasFocus();
                return;
            }
            intProtein =   Integer.parseInt(etProtein.getText().toString());
            intFiber = Integer.parseInt(etFiber.getText().toString());
            intCarbs = Integer.parseInt(etCarbs.getText().toString());
            calculate();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }


    };

    etFat.addTextChangedListener(watcher);
    etProtein.addTextChangedListener(watcher);
    etFiber.addTextChangedListener(watcher);
    etCarbs.addTextChangedListener(watcher);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_wwcalc, menu);

return true;

}
public void calculate(){
    //intTot = intFat + intCarbs + intFiber + intProtein;
    intTot = (int) Math.ceil((intFat * (4/35)) + (intCarbs * (4/36.84)) - (intFiber* (4/50))+ (intProtein * (4/43.75)) ) ;
    tvTotal.setText(Integer.toString(intTot));
}
private boolean isEmpty(EditText etText)
{
 if(etText.getText().toString().trim().length() > 0 || etText.getText().toString().trim() != null)
    return false;
 else
   return true; 
}

}

Thanks for the help all. I got it working not sure if it is the best solution if anyone thinks there is a better way let me know. The try catch as suggested by conor catches the exception, then just insert a 0 set focus and select the 0

try {
                intFat = Integer.parseInt(etFat.getText().toString());
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                etFat.setText("0");
                etFat.hasFocus();
                etFat.selectAll();
            }
  • 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-17T22:44:01+00:00Added an answer on June 17, 2026 at 10:44 pm

    Without seeing the stacktrace it is hard to know but i’m going to make a stab it anyway.

    The following piece of your isEmpty() function is allowing false to be returned when the box could still be emtpy.

    etText.getText().toString().trim() != null
    

    This means that when you press backspace and clear the field it is empty but your function says it’s not. Then the kicker, when your app thinks the field is not empty it tried to parse the contents for an integer value (which is not present). This attempt at parsing throws an exception and crashes your app.

    I expect the stacktrace to show the app crashing at this line

    intFat = Integer.parseInt(etFat.getText().toString());
    

    It’s worth noting that you should always surround calls like Integer.parseInt() with a try, catch block.

    Hope that helps.

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

Sidebar

Related Questions

I'm trying to learn web development in Java and, from what I have seen
I am trying to learn more about MySQL and using Java (on Android) to
I'm building a web program in Dreamweaver that uses Java and I am trying
I'm trying to learn Java/Android and right now I'm doing some experiments with the
I'm very very new to android & JAVA, and I'm trying to learn writing
I have been trying to learn as much as possible about Android development with
I am trying to learn Java. I would like to implement a simple networked
I was trying to learn java and when I went through access specifiers I
I am trying to learn Java's threads in order to do an assignment, but
greetngs, i am trying to learn Java and Swing by writing a simple game

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.