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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:21:19+00:00 2026-05-28T03:21:19+00:00

I am trying to build a calculator for android, i am trying to multiply

  • 0

I am trying to build a calculator for android, i am trying to multiply two numbers.
If i give number 1 and leave number 2 open, i want to get zero as an answer.

I first thought it could be the problem of not having a value for the double.
So i tried: if (Double.isNaN(number1)) { number1=0; }
But it did not work.

How do i make this happen?

R.layout.main:

         <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="number:" />
    <EditText
        android:id="@+id/number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="0"
        android:inputType="number"
        android:layout_marginLeft="20dp" />
</LinearLayout>

<!-- 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="number:" />
    <EditText
        android:id="@+id/number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="0"
        android:inputType="number"
        android:layout_marginLeft="20dp" />
</LinearLayout>


<!-- multiply -->   
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"  >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="multiply:" />
    <TextView
        android:id="@+id/multiplydisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_marginLeft="20dp" />
</LinearLayout>


<Button
    android:id="@+id/btncalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate"/>

<Button
    android:id="@+id/btnreset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset" />

     </LinearLayout>

TipcalcActivity.java

package com.tip.calc;

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


    public class TipcalcActivity extends Activity   {
private EditText number1;
private EditText number2;
private TextView multiplydisplay;
private Button btncalculate;
private Button btnreset;
private double number1calc = 0;
private double number2calc = 0;
private double multiply     = 0;


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

private void initControls() {
    number1 = (EditText)findViewById(R.id.number1);
    number2 = (EditText)findViewById(R.id.number2);
    multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
    btncalculate = (Button)findViewById(R.id.btncalculate);
    btnreset = (Button)findViewById(R.id.btnreset);
    btncalculate.setOnClickListener(new Button.OnClickListener() { public void 
     onClick (View v){ calculate(); }});
    btnreset.setOnClickListener(new Button.OnClickListener() { public void   
  onClick (View v){ reset(); }});
}

private void calculate()    {
    number1calc=Double.parseDouble(number1.getText().toString());
    number2calc=Double.parseDouble(number2.getText().toString());
    multiply=(number1calc*number2calc);
    multiplydisplay.setText(Double.toString(multiply));
}

private void reset()    {
    multiplydisplay.setText("");
    number1.setText("");
    number2.setText("");
}
}
  • 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-28T03:21:20+00:00Added an answer on May 28, 2026 at 3:21 am
    if(numberOne.getText().toString().trim().length() < 1 || numberTwo.getText().toString().length() < 1){
        multiplyDisplay.setText("0");
    }
    else{
        //default handling
    }
    

    If the edittext is left blank, this is obviously not a number(NaN). Doing any operation with NaN in Java will cause the result of this operation to be NaN.

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

Sidebar

Related Questions

I am trying to build a Retirement Calculator as a chance to make something
I am trying to build a simple calculator to learn javascript/jquery but seem to
I'm trying to build a litle gam for android GSMs. I hava lots of
I am trying to build a calculator consists of a set of values are
I'm trying to get the calculator app I've built to trigger some simple code
I am trying to get an exchange rate from the iGoogle Calculator. I have
I'm trying to build a scientific calculator with vb .net, except it is vista
Im trying to build a calculator with PyQt4 and connecting the 'clicked()' signals from
I'm trying to build a calculator GUI using migLayout, but I'm not familiar with
I'm trying to use JavaCC to build a simple command line calculator that can

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.