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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:53:59+00:00 2026-05-27T05:53:59+00:00

I’m trying to get the a, b, and c values as floats from edit

  • 0

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;
        }
    }
}
  • 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-27T05:54:00+00:00Added an answer on May 27, 2026 at 5:54 am

    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);
                    }
                });  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.