-3

Here are my codes

public void onClick(View v)
{
    if (v == buttonOne)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("1");
    }

    else if (v == buttonTwo)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("2");
    }

    else if (v == buttonThree)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("3");
    }

    else if (v == buttonFour)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("4");
    }

    else if (v == buttonFive)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("5");
    }

    else if (v == buttonSix)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("6");
    }

    else if (v == buttonSeven)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("7");
    }

    else if (v == buttonEight)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("8");
    }

    else if (v == buttonNine)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("9");
    }

    else if (v == buttonZero)
    {
        TextView output = (TextView)findViewById(R.id.output);
        output.append("0");
    }

    else if(v == buttonEnter)
    {

        TextView output = (TextView)findViewById(R.id.output);
        temp = Integer.parseInt(output.getText().toString());
        compareNumber(temp);
        output.setText("");
    }
}

I am trying to compare number using button. For example, if I press buttonOne it append 1 to stack. It works completely fine when enter is pressed after one or more number is clicked, but the application stops when I left the EditText without entering any numbers and press buttonEnter.

Please help!

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
Eric
  • 159
  • 14
  • 2
    Please read this: http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors And come back and post your stack trace – Daniel Nugent Feb 08 '16 at 03:54
  • 1
    And this too: http://stackoverflow.com/q/23353173/1270789 – Ken Y-N Feb 08 '16 at 03:58

3 Answers3

2

The problem is at this line

temp = Integer.parseInt(output.getText().toString());

When you try to use parseInt() to the empty String. It will crash up. Have a look at your variable output.

Solution

You can always apply a condition before doing something with value that was retrieved from EditText by adding this line

if(editText.getText().length() != 0)
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
1

You will have to check whether TextView is empty or not, before parsing it into int. You can do the check as follows.

else if(v == buttonEnter)
    {
        TextView output = (TextView)findViewById(R.id.output);
        String txt = output.getText().toString();
        if (!temp.matches("")) {
            temp = Integer.parseInt(output.getText().toString());
    }

Also, why are you declaring the Textview in each if loops? You can do it as follows

public void onClick(View v)
 {

    TextView output = (TextView)findViewById(R.id.output);
    if (v == buttonOne)
    {
        output.append("1");
    }
    else if (v == buttonTwo)
    {
        output.append("2");
    }

    else if (v == buttonThree)
    {
        output.append("3");
    }

    else if (v == buttonFour)
    {
        output.append("4");
    }

    else if (v == buttonFive)
    {
        output.append("5");
    }

    else if (v == buttonSix)
    {
        output.append("6");
    }

    else if (v == buttonSeven)
    {
        output.append("7");
    }

    else if (v == buttonEight)
    {
        output.append("8");
    }

    else if (v == buttonNine)
    {
        output.append("9");
    }

    else if (v == buttonZero)
    {
        output.append("0");
    }

    else if(v == buttonEnter)
    {
        temp = Integer.parseInt(output.getText().toString());
        compareNumber(temp);
        output.setText("");
    }
}
Lal
  • 14,726
  • 4
  • 45
  • 70
0

This is just to make your code look nice. Create an integer array for your textView. Take for example

private int[] numericBtn = {R.id.idzero,R.id.one, R.id.two, R.id.three, R.id.four,R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine};


private int[] operationBtn = {R.id.buttonAdd,R.id.buttonMinus, R.id.buttonMultiply, R.id.buttonDivide};

Create an onclick listener for your numeric Button. Like this

public void setNumericOnClickListener() {
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button id = (Button) v;                
            output.append(id.getText());

        }
    };

    for (int id : numericBtn) {
        findViewById(id).setOnClickListener(listener);
    }

}

Then call this method at the OnCreate method. Same goes for the operatorButtons. Happy coding.

Jeremiah
  • 324
  • 3
  • 13