steven
steven

Reputation: 698

Calculation error? Or something else?

So I'm not sure if I'm just blind to the problem or what's going on. From everything I have checked this should be working just fine. I'm guessing there is something tiny in here that I am not noticing and I am hoping someone else will catch it because I'm not seeing it. Basically the first class has spinners and it takes input and saves them all, loading up the old information and adding the new to them. Then it does a calculation to give me the new double for one of them. The second class is what displays them. For some reason it is constantly displaying the onbasePerc as 0.000. The calculation that it should be doing should be giving me a number and I am not sure why it is not.

Here is the input class

public class AddGameBR extends Activity{

private static final String[] stealAttempt = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] stolenBase = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] caughtSteal = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] runs = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};

double stolenPerc=0;
int sa=0, sb=0, caught=0, run=0;

double ostolenPerc=0;
int osa=0, osb=0, ocaught=0, orun=0;

double nstolenPerc=0;
int nsa=0, nsb=0, ncaught=0, nrun=0;

public static final String PREFS_NAME = "GameSaved";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addgamebr);

    oldStats();

    //Baserunning Stats

    final Spinner spin16 = (Spinner) findViewById(R.id.spin16);
    ArrayAdapter<String> spinn16 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, stealAttempt);
    spinn16.setDropDownViewResource(R.layout.spinner_entry);
    spin16.setAdapter(spinn16);

    final Spinner spin17 = (Spinner) findViewById(R.id.spin17);
    ArrayAdapter<String> spinn17 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, stolenBase);
    spinn17.setDropDownViewResource(R.layout.spinner_entry);
    spin17.setAdapter(spinn17);

    final Spinner spin18 = (Spinner) findViewById(R.id.spin18);
    ArrayAdapter<String> spinn18 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, caughtSteal);
    spinn18.setDropDownViewResource(R.layout.spinner_entry);
    spin18.setAdapter(spinn18);

    final Spinner spin19 = (Spinner) findViewById(R.id.spin19);
    ArrayAdapter<String> spinn19 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, runs);
    spinn19.setDropDownViewResource(R.layout.spinner_entry);
    spin19.setAdapter(spinn19);

    Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View calculate)
        {
             //Running


            sa = Integer.parseInt((String) spin16.getSelectedItem());
            sb = Integer.parseInt((String) spin17.getSelectedItem());
            caught = Integer.parseInt((String) spin18.getSelectedItem());
            run = Integer.parseInt((String) spin19.getSelectedItem());

            nsa = sa + osa;
            nsb = sb + osb;
            ncaught = caught + ocaught;
            nrun = run + orun;

            if(nsa==0)
                nstolenPerc=0;
            else
                nstolenPerc = nsb / nsa;

            if(sa<sb){
                AlertDialog.Builder builder = new AlertDialog.Builder(AddGameBR.this);
                builder.setMessage("You have more Stolen Bases than Steal Attempts. Please fix and then resubmit.")
                       .setCancelable(false)
                       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                builder.show();
            }
            else{

                AlertDialog.Builder builder = new AlertDialog.Builder(AddGameBR.this);
                builder.setMessage("Are you sure these stats are correct? \n\nAfter submitting they are final.")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                               SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
                                SharedPreferences.Editor editor = settings.edit();
                                saveGame(editor);
                                editor.commit();

                               Intent i = new Intent(AddGameBR.this, ViewBR.class);
                                startActivity(i);
                                finish();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                builder.show();
            }
        }
    });

}

public void saveGame(SharedPreferences.Editor map){
    if (map == null) {          
        return;
    }
    //Baserunning
            map.putInt("stealattempts", nsa);
            map.putInt("stolenbases", nsb);
            map.putInt("caughtstealing", ncaught);
            map.putInt("run", nrun);
            map.putString("stealPercentage", Double.toString(nstolenPerc));
}

public void oldStats(){
     SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
    //Baserunning
        osa = saved.getInt("stealattempts", 0);
        osb = saved.getInt("stolenbases", 0);
        ocaught = saved.getInt("caughtstealing", 0);
        orun = saved.getInt("run", run);
        ostolenPerc = Double.parseDouble(saved.getString("stealPercentage", "0"));
}

}

This is the viewing class

public class ViewBR extends Activity{
public static final String PREFS_NAME = "GameSaved";

double stolenPerc=0;
int sa=0, sb=0, caught=0, run=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewbr);

// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    oldStats();

    final DecimalFormat formatter = new DecimalFormat("0.000");

    if(sa==0)
        stolenPerc=0;
    else
        stolenPerc = sb / sa;

  //Base Running

    TextView stealattempts = (TextView)findViewById(R.id.stealattempts);
    stealattempts.setText("Steal Attempts: " + sa);

    TextView stolenbases = (TextView)findViewById(R.id.stolenbases);
    stolenbases.setText("Stolen Bases: " + sb);

    TextView caughtstealing = (TextView)findViewById(R.id.caughtstealing);
    caughtstealing.setText("Caught Stealing: " + caught);

    TextView runs = (TextView)findViewById(R.id.runs);
    runs.setText("Runs: " + run);

    TextView stolenpercentage = (TextView)findViewById(R.id.stolenpercentage);
    stolenpercentage.setText("Stolen Base Percentage: " + formatter.format(stolenPerc) + "%");

    Button copy = (Button) findViewById(R.id.copy_btn);
    copy.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View copy)
        {
            ClipboardManager clipboard = 
                      (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 

                 clipboard.setText("Base Running Stats:" +
                        "\nSteal Attempts: " + sa+
                        "\nStolen Bases: " + sb +
                        "\nCaught Stealing: " + caught +
                        "\nRuns: " + run +
                        "\nStolen Base Percentage: " + formatter.format(stolenPerc) + "%");
                 Toast.makeText(ViewBR.this, "Copied To Clipboard", 1).show();
        }
    });


}

public void oldStats(){
     SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);

    //Baserunning
        sa = saved.getInt("stealattempts", 0);
        sb = saved.getInt("stolenbases", 0);
        caught = saved.getInt("caughtstealing", 0);
        run = saved.getInt("run", run);
        stolenPerc = Double.parseDouble(saved.getString("stealPercentage", "0"));
}

}

Any help would be appreciated. It's probably going to be something so simple I am going to end up smacking myself in the face. Anyways take care. (Also when I am inputting it all I am not submitting it with sa as zero normally I am trying sa as 4 and sb as 2 which should be .500.

Upvotes: 0

Views: 126

Answers (1)

Jack
Jack

Reputation: 9242

Have you debugged to step through teh codez and find out exactly what the values are before the problem method returns a bad value? Also - I may be off here but integer division will return an integer (your expected value is .5, it will be 0 instead). See this question.

Upvotes: 1

Related Questions