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

  • Home
  • SEARCH
  • 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 7027169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:45+00:00 2026-05-28T00:12:45+00:00

I’m new to Java and Android development so I decided to make a simple

  • 0

I’m new to Java and Android development so I decided to make a simple app that takes in two fields and puts them in a simple math equation. However I have to convert the EditText into Int before I can do any math with them, right?
In the XML file I have set both EditTexts to android:inputType=”number”.
My attempts have been like this:

  final EditText height = (EditText) findViewById (R.id.height);
  String heightString = height.getText().toString();
  final int heightInt = Integer.parseInt(heightString);

and

  final EditText height = (EditText) findViewById (R.id.height);
  final int heightInt = Integer.parseInt(height.getText().toString());

I also tried Integer.valueOf in both cases but it always force closes my app when I run it and if I comment out that int heightInt it doesn’t force close.

Here’s the whole code:

package com.body.calculator;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class bmi extends Activity {
     /** Called when the activity is first created. */



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView results = (TextView) findViewById(R.id.results);
    final EditText height = (EditText) findViewById (R.id.height);
    String heightString = height.getText().toString();
    final int heightInt = Integer.parseInt(heightString);
    final EditText weight = (EditText) findViewById (R.id.weight);
    final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    Button submit_bmi = (Button) findViewById(R.id.submit_bmi);
    submit_bmi.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (height.getText().toString().equals("") && weight.getText().toString().equals("")) {
                results.setText("Both fields are empty");
                vib.vibrate(100);
            }
            else if (height.getText().toString().equals("")) {
                results.setText("Height is empty");
                vib.vibrate(100);
            }
            else if (weight.getText().toString().equals("")) {
                results.setText("Weight is empty");
                vib.vibrate(100);
            }
            else {
            results.setText("");    
            }
        }


    });

}
}

Any help would be appreciated.
Thanks, Arnar.

  • 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-05-28T00:12:46+00:00Added an answer on May 28, 2026 at 12:12 am

    You approach of

      final EditText height = (EditText) findViewById (R.id.height);
      String heightString = height.getText().toString();
      final int heightInt = Integer.parseInt(heightString);
    

    is correct, but you are calling this directly in your onCreate method. This means they get called as soon as the app starts, even before the user is able to do anything. because of this the values are always empty, simply because the user hasn’t even had the change to do anything yet.

    When you use the values later in your onClick(which is correct), they are still empty due to their early initialization.

    Try putting this part:

    String heightString = height.getText().toString();
    final int heightInt = Integer.parseInt(heightString);
    

    Directly after the start of your onClick method but before your if/else statements like so:

    public void onClick(View v) {
        String heightString = height.getText().toString();
        final int heightInt = Integer.parseInt(heightString);
    
               // Your statements
    
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.