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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:29:30+00:00 2026-05-26T00:29:30+00:00

I’m realy beginning to learn Java. When I run this code everything works fine

  • 0

I’m realy beginning to learn Java.
When I run this code everything works fine till I leave my EditText boxes in the from empty and hit the run button. Then I get:

ERROR/AndroidRuntime(866): FATAL EXCEPTION: main
ERROR/AndroidRuntime(866): java.lang.NumberFormatException: 
ERROR/AndroidRuntime(866):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)

I guess when I try to parse a Double from string it doesn’t get any value and creates an error.
I was wondering how to avoid this error and give some kind of a value to the variable so it is never empty? Thanks for all the help!

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class Main extends Activity {

    private EditText noKids;
    private EditText noGumballs;
    private TextView noSum;
    private Button b;
    private Button br;
    double etKids = 0;
    double etGumballs = 0;
    private double tvSumIn = 0.00;

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


    private void initControls() {
        noKids = (EditText) findViewById(R.id.xetKids);
        noGumballs = (EditText) findViewById(R.id.xetGumballs);
        noSum = (TextView) findViewById(R.id.tvSum);


        br = (Button) findViewById(R.id.xbtnReset);
        br.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                reset();
                br.setVisibility(View.INVISIBLE);
            }

            private void reset() {
                noKids.setText("");
                noGumballs.setText("");
                noSum.setText("");

            }

        });

        b = (Button) findViewById(R.id.xbtnCalculate);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {               
                calculate();
                br.setVisibility(View.VISIBLE);
                }

        private void calculate() {

            etKids = Double.parseDouble(noKids.getText().toString());
            etGumballs = Double.parseDouble(noGumballs.getText().toString());
            tvSumIn = etGumballs / etKids; 
            String thisIsIt = new Double(tvSumIn).toString();

            if(tvSumIn < 2){
                noSum.setText(thisIsIt + " This is it ");
            }else{
                noSum.setText("This is else");
            }

        }

        });



        }
    }

This is my main.xml file
and I’m guessing there’s nothing special to put into manifest.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="How many kids have you got?"></TextView>
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:id="@+id/xetKids">
        <requestFocus></requestFocus>
    </EditText>
    <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="How many gumballs have you got?"></TextView>
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:id="@+id/xetGumballs"></EditText>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" android:id="@+id/xbtnCalculate"></Button>
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvSum"></TextView>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/xbtnReset" android:text="Reset" android:onClick="selfDestruct" android:visibility="invisible"></Button>
</LinearLayout>
  • 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-26T00:29:30+00:00Added an answer on May 26, 2026 at 12:29 am

    Here is the JavaDoc for java.lang.Double class, parseDouble method:

    /**
     * Parses the specified string as a double value.
     * 
     * @param string
     *            the string representation of a double value.
     * @return the primitive double value represented by {@code string}.
     * @throws NumberFormatException
     *             if {@code string} is {@code null}, has a length of zero or
     *             can not be parsed as a double value.
     * @since Android 1.0
     */
    

    Empty values are not considered to be parseable. That’s why you get this exception.

    You can intoduce an additional check to your code to see if string in the noKids EditText is empty and if so, manually set the value to 0.0:

    noKidsStr = noKids.getText().toString();
    
    if(noKidsStr == null || noKidsStr.isEmpty()) {
    
      etKids = 0.0;
    
    } else {
    
      etKids = Double.parseDouble(noKids.getText().toString());
    
    }
    

    I suggest writing some sort of convenience utility method that you can re-use for all similar situations in the future.

    • 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 &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.