shivamDev shivamDev - 3 months ago 72
Android Question

Factorial program in android

Hey guys I know there are many java programs for the factorial of a number...but I am facing problem with android. Following is my code....Thanks

package com.droidacid.apticalc.aptitudes;

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

import com.droidacid.apticalc.R;

public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;

long factorial = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apti_factorial);
initialize();
calcFactorial();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private void calcFactorial() {
if (number.getText().toString().equals("")) number.setText("0");
int num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}

}


@Override
public void onClick(View v) {
calcFactorial();
answer.setText("Factorial of " + number.getText().toString() + " is : " + factorial);

}

}


This is my code and I need to know a way around for setting the hint instead of 0, but if I remove
if (number.getText().toString().equals("")) number.setText("0");


then getting NullPointerException...
Also on the first go its calculating correctly but If I calculate again then getting wrong answer. I think some loop issue because I am directly using the value of factorial.

Please help thanks

Answer

Forget all that equal stuff, cover all basis and do it like this:

package com.droidacid.apticalc.aptitudes;

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

import com.droidacid.apticalc.R;

public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apti_factorial);
    initialize();
}

private void initialize() {
    number = (EditText) findViewById(R.id.et_apti_number);
    number.setHint("Enter number to be factorialized :P")
    answer = (TextView) findViewById(R.id.tv_apti_answer);
    calculate = (Button) findViewById(R.id.b_apti_calc);
    calculate.setOnClickListener(this);
}

private long calcFactorial() {

    long factorial = 1;
    try {
        factorial = Long.parseLong(number.getText().toString());
        for(int i=factorial-1; i>0; i--){
            factorial = i * factorial;
        }
    } catch (NumberFormatException e) {
        Toast.makeText(this, "Incorrect Input", Toast.LENGTH_LONG).show();
    } finally {}

    return factorial;
}

@Override
public void onClick(View v) {
    answer.setText("Factorial of " + number.getText().toString() + " is : " + calcFactorial());
}