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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:19:04+00:00 2026-05-29T04:19:04+00:00

I am very new to Java as well as Android programming. I was attempting

  • 0

I am very new to Java as well as Android programming. I was attempting to make a calculator. But as soon as I inserted the square root button, the application crashes whenever I hit divide or square root. I don’t know where I’m going wrong. Help,please?

public class MainScreen extends Activity implements OnClickListener {
      EditText edit1, edit2;
      TextView txt;
      Button butt, diff, mul, div, sqr, per;


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


        edit1 = (EditText)findViewById(R.id.editText1);
        edit2 = (EditText)findViewById(R.id.editText2);


        txt = (TextView)findViewById(R.id.textView1);



        butt = (Button)findViewById(R.id.button1);
        butt.setOnClickListener(this);
        diff = (Button)findViewById(R.id.button2);
        diff.setOnClickListener(this);
        mul = (Button)findViewById(R.id.button3);
        mul.setOnClickListener(this);
        div = (Button)findViewById(R.id.button4);
        div.setOnClickListener(this);
        sqr = (Button)findViewById(R.id.button5);
        sqr.setOnClickListener(this);
        per = (Button)findViewById(R.id.button6);
        per.setOnClickListener(this);
        }


    @Override
    public void onClick(View v) {
        String a;
        // TODO Auto-generated method stub
        String b;
        Integer sum, sub, mul, div;
        Double x ;



        a= edit1.getText().toString();
        b= edit2.getText().toString();
        x=(double) Integer.parseInt(a);

        switch (v.getId()) {

            case R.id.button1:
            sum = Integer.parseInt(a)+ Integer.parseInt(b);
            txt.setText(sum.toString());
            break;

            case R.id.button2:
            sub = Integer.parseInt(a)- Integer.parseInt(b);
            txt.setText(sub.toString());
            break;

            case R.id.button3:
            mul = Integer.parseInt(a)* Integer.parseInt(b);
            txt.setText(mul.toString());
            break;

            case R.id.button4:
            div = (Integer.parseInt(a))/(Integer.parseInt(b));
            txt.setText(div.toString());

            case R.id.button5:
            txt.setText((int) Math.sqrt(x));


            default:
            break;
        }}}
  • 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-29T04:19:05+00:00Added an answer on May 29, 2026 at 4:19 am
    1. It’s bad idea to use (int) Math.sqrt(x) – sqrt does not return integer.

    2. When you write textView.setText(int) – Andrid thinks, that you provide id of string resource, and it can’t find it and crashes.

    Use this:

    case R.id.button5:
        txt.setText(String.valueOf(Math.sqrt(x)));
        break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Android programming and to Java in general, but I have
I'm very new to Java but I've been developing a habit to use final
I'm soon going to check in the very first commit of a new Java
Im very new to C programing, but have limited experience with Python, Java, and
Allright som first of im very new to android programming and was trying to
I am new to Android/java programming. I really have no clue what I am
I am very new to Java, I want to add a PNG image to
I am very new to processing.org and Java. I am trying to store objects
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I am really new to Java and I read that synchronized is very expensive

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.