ninge
ninge

Reputation: 1598

How to take the value of two EditText fields and perform simple mathematics

I am new to Android development and am working on a simple practice project where the user enters a number into two EditText fields and presses a Button labeled "Calculate" and the sum of the two numbers is displayed. Here is the code I have so far, but I don't know how to add the two string values and output it to a TextView field named "answer":

public void calNumbers(View view) {
    EditText text = (EditText)findViewById(R.id.edit_number1);
    String value = text.getText().toString();       

    EditText text2 = (EditText)findViewById(R.id.edit_number2);
    String value2 = text2.getText().toString();              

    TextView answer = (TextView) findViewById(R.id.answer);      
}

Upvotes: 1

Views: 8541

Answers (4)

Leonardo Meneses
Leonardo Meneses

Reputation: 11

you can get the value from your EditText in many ways, but if what you want to do is make operations make sure of these things first. in the layout of the XML of your EditText make sure android:inputType is set to "number", "numberSigned" or "numberDecimal" so the user doesn't mess with our app.

Now on the java class initialize globally the arguments of your number to 0 depending on your expected output, either integer (int) for sums or substractions between integers (inputType number or numberSigned) or more general as a double for all operations.

Finally what you need to do is parsing the information, not getting it as a text String because it involves two unnecessary transformations.

public void wattSystemView(View view) {

/**
* decimal values entered in hor_editText and wh_editText
* number of days without sun of the system as a whole number at days_editText
*/

hours     = (EditText) findViewById(R.id.hor_editText);
wh        = (EditText) findViewById(R.id.wh_editText);
daysNoSun = (EditText) findViewById(R.id.nosun_days_editText);

/**
* conversion to double of the decimal values entered 
* conversion to integer of the number of days without sun of the system
*/
numh      = Double.parseDouble(hours.getText().toString());
numwh     = Double.parseDouble(wh.getText().toString());
nosundays = Integer.parseInt(daysNoSun.getText().toString());

    /**
    * simple math 
    */
    double wattDay = numh * numwh;
    double wattSys = wattDay*nosundays;

Upvotes: 1

Erik-RW
Erik-RW

Reputation: 173

Cast the String to long or whatever you need

public void calNumbers(View view) {
    EditText text = (EditText)findViewById(R.id.edit_number1);
    String value = text.getText().toString();       

    EditText text2 = (EditText)findViewById(R.id.edit_number2);
    String value2 = text2.getText().toString();              

    TextView answer = (TextView) findViewById(R.id.answer);      
    long l1 = Long.parseLong(text);
    long l2 = Long.parseLong(text2);

    long result = l1 + l2;
    answer.setText(Long.toString(result));
}

Upvotes: 4

FoamyGuy
FoamyGuy

Reputation: 46856

You'll have to convert them to numbers (int, float, long, etc) to preform your arithmetic. Then convert the result back to a String to display in a TextView

int val1 = Integer.parseInt(value);
int val2 = Integer.parseInt(value2);

answer.setText(String.valueOf(val1 + val2));

Upvotes: 8

Jeje Doudou
Jeje Doudou

Reputation: 1706

To get the integer value of a string:

final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2);

Then you can perform your addition and store the result in a variale. Then to display the result as a string:

answer.setText(Integer.toString(myIntResult));

Upvotes: 5

Related Questions