bensherms
bensherms

Reputation: 83

EditText To Float Not Returning Values

I'm trying to get the a, b, and c values as floats from edit text strings, but they don't appear to update the quadratic class when a new one is created.

Android Code. In the first part, "savenum," is where I'd expect my a, b, and c floats to be updated. When I call them later with the method final Quadratic quad = new Quadratic (a, b, c), but they don't appear to be updated. The values that are presented in the dialog boxes that should show the values entered in the edittext boxes are 0.0. :

package com.bensherman.quadroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class QuadraticdroidActivity extends Activity {
    /** Called when the activity is first created. */

    private EditText entA, entB, entC;
    float a, b, c;

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

        SharedPreferences QuadPref = this.getSharedPreferences("QUADROID", MODE_PRIVATE);
        final SharedPreferences.Editor QuadEdit = QuadPref.edit();

        Button savenum = (Button) findViewById(R.id.btnSave);
        savenum.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                       entA = (EditText) findViewById(R.id.entA);  
                       float a = Float.parseFloat(entA.getText().toString()); 
                       QuadEdit.putFloat("a", a).commit();

                       entB = (EditText) findViewById(R.id.entB);  
                       float b = Float.parseFloat(entB.getText().toString()); 
                       QuadEdit.putFloat("b", b).commit();

                       entC = (EditText) findViewById(R.id.entC);  
                       float c = Float.parseFloat(entC.getText().toString()); 
                       QuadEdit.putFloat("c", c).commit();
                    }
                });  

        final Quadratic quad = new Quadratic(a, b, c);

        final Builder showX = new AlertDialog.Builder(this);
        Button getX =  (Button) findViewById(R.id.getXb);
        showX.setTitle("X Value");
        getX.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showX.setMessage("X: " + Math.round(quad.getXvalue()));
                showX.show();
            }
        });

        final Builder showY = new AlertDialog.Builder(this);
        Button getY =  (Button) findViewById(R.id.getYb);
        showY.setTitle("Y Value");
        getY.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showY.setMessage("Y: " + Math.round(quad.getYvalue()));
                showY.show();
            }
        });

        final Builder showA = new AlertDialog.Builder(this);
        Button getA = (Button) findViewById(R.id.getAb);
        showA.setTitle("A Value");
        getA.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showA.setMessage("A: " + quad.getA());
                showA.show();
            }
        });

        final Builder showB = new AlertDialog.Builder(this);
        Button getB = (Button) findViewById(R.id.getBb);
        showB.setTitle("B Value");
        getB.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showB.setMessage("B: " + quad.getB());
                showB.show();
            }
        });

        final Builder showC = new AlertDialog.Builder(this);
        Button getC = (Button) findViewById(R.id.getCb);
        showC.setTitle("C Value");
        getC.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showC.setMessage("C: " + quad.getC());
                showC.show();
            }
        });

        final Builder showSF = new AlertDialog.Builder(this);
        Button getSF = (Button) findViewById(R.id.getSFb);
        showSF.setTitle("Standard Form");
        getSF.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showSF.setMessage("Stand. Form: " + quad.getStandardForm());
                showSF.show();
            }
        });
    } 
}  

XML There are 3 edittext fields that are numberSigned to allow for negatives. Then there are several buttons that when clicked, show the methods in the Quadratic class. :

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

    <EditText
        android:id="@+id/entA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="@string/ahint"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/entB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entA"
        android:hint="@string/bhint"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/entC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entB"
        android:hint="@string/chint"
        android:inputType="numberSigned" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entC"
        android:text="@string/savebutton" />

    <Button
        android:id="@+id/getXb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnSave"
        android:text="@string/getX" />

    <Button
        android:id="@+id/getAb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/entC"
        android:text="@string/getA" />

    <Button
        android:id="@+id/getCb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/getXb"
        android:text="@string/getC" />

    <Button
        android:id="@+id/getBb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/getAb"
        android:text="@string/getB" />

    <Button
        android:id="@+id/getYb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/getCb"
        android:layout_alignBottom="@+id/getCb"
        android:layout_alignParentLeft="true"
        android:text="@string/getY" />

    <Button
        android:id="@+id/getSFb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/getYb"
        android:text="@string/getSF" />

</RelativeLayout>

Quadratic Class. In this class, a quadratic is being made. There are several method calls for different things to do or tell about the quadratic. In the android activity, this class is called to for each function.:

package com.bensherman.quadroid;


public class Quadratic {

    private double a, b, c, y, x, s;

    public Quadratic()  
    {
        a = 1;
        b = 2;
        c = -1;
    }

    public Quadratic (double myA, double myB, double myC)
    {
        a = myA;
        b = myB;
        c = myC;
    }
    public double getA()
    {
        return a;
    }

    public double getB()
    {
        return b;
    }

    public double getC()
    {
        return c;
    }

    public double getSolution1()
    {
        return (-(b) + Math.sqrt((Math.pow(b, 2))-4*a*c))/(2*a);
    }

    public double getSolution2()
    {
        return (-(b) - Math.sqrt((Math.pow(b, 2))-4*a*c))/(2*a);
    }

    public double getXvalue()
    {
        return (-b)/(2*a);
    }

    public double getYvalue()
    {
        return (a*(Math.pow(x,2))) + (b*x) + c;
    }

    public String getStandardForm()
    {
        if (c < 0){
            return a + "x^2 + " + b + "x " + c;
        }
        else
        {
            return a + "x^2 + " + b + "x +" + c;
        }
    }
}

Upvotes: 1

Views: 1104

Answers (1)

Chris
Chris

Reputation: 23171

When you call final Quadratic quad = new Quadratic(a, b, c); in onCreate, a b and c have not yet been initialized so they will always be 0. The code inside your button handler does not execute until the button is pressed so even though it appears before the final Quadratic quad = new Quadratic(a, b, c); line in the source, it hasn't yet been executed.

Beyond that, even if it was executed, a, b and c would still be uninitialized since you're declaring local variables with the same name and assigning to those rather than using the instance variables that you've already defined.

What you can do is move quad from being a local variable to an instance variable. Then you can re-initialize it in the button handler when you update a,b,c from the edit texts:

public class QuadraticdroidActivity extends Activity {

    private Quadratic quad;
    private EditText entA, entB, entC;
    float a, b, c;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       SharedPreferences QuadPref = this.getSharedPreferences("QUADROID", MODE_PRIVATE);

       final SharedPreferences.Editor QuadEdit = QuadPref.edit();
       a = QuadEdit.getFloat("a",0f);
       b = QuadEdit.getFloat("b",0f);
       c = QuadEdit.getFloat("c",0f);
//initialize here since the initialization in the button handler won't happen until someone clicks the button
           quad = new Quadtratic (a,b,c);
       Button savenum = (Button) findViewById(R.id.btnSave);
       savenum.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                   entA = (EditText) findViewById(R.id.entA);  
                   //got rid of the "float" here so now we're referring to the instance     variable instead of a local
                   a = Float.parseFloat(entA.getText().toString()); 
                   QuadEdit.putFloat("a", a).commit();

                   entB = (EditText) findViewById(R.id.entB);  
                   b = Float.parseFloat(entB.getText().toString()); 
                   QuadEdit.putFloat("b", b).commit();

                   entC = (EditText) findViewById(R.id.entC);  
                   c = Float.parseFloat(entC.getText().toString()); 
                   QuadEdit.putFloat("c", c).commit();

                   quad = new Quadratic(a,b,c);
                }
            });  

Upvotes: 1

Related Questions