-1

I want to make equation between two edit text and put the output on Toast I tried to use that way : String data1 = one.getText().toString(); String data2 = two.getText().toString(); String RES = data1 + data2; but this way outputted result as String not numbers Find code below : Main Activity code : MainActivity.java

package com.mkadaim.android;

import android.R.integer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends ActionBarActivity {

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


    final EditText one = (EditText)findViewById(R.id.First);
    final EditText two = (EditText)findViewById(R.id.Second);

    Button res = (Button)findViewById(R.id.Result);
    res.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        int x = one.getText();
        int y = two.getText();
        int z = x + y;
        Toast.makeText(getBaseContext(), z, 4000).show();

        }
    });
}


}
MKADAIM
  • 33
  • 1
  • 6

4 Answers4

0

one.getText() returns the id of the EditText. Use this instead.

@Override
public void onClick(View v) {
    int x = Integer.parseInt(one.getText().toString());
    int y = Integer.parseInt(two.getText().toString());
    int z = x + y;
    Toast.makeText(getBaseContext(), z, 4000).show();
Adam Wigren
  • 383
  • 2
  • 11
0

You would need to parse String to Integer as follows before evaluation:

res.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        String xStr = one.getText().toString().trim();
        String yStr = two.getText().toString().trim();

        if(!(xStr.equals("") && yStr.equals(""))){
            int x = Integer.parseInt(xStr);    //Try catch for handling parse exceptions for both String
            int y = Integer.parseInt(yStr); 
            int z = x + y;
            Toast.makeText(context, String.valueOf(z), 4000).show();
        }
        else{
            //enter both numbers
        }
    }
});

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • A better approach would be to check if `xStr` and `yStr` is not equal to nulls e.g. `if (null != xStr) { // codes here }`, as opposed to a blank value. – ChuongPham Sep 27 '14 at 12:38
  • Can you explain me this code ` if(!(xStr.equals("") && yStr.equals("")))` and what are you mean by "Try catch for handling parse exceptions for both String" – MKADAIM Sep 27 '14 at 12:54
  • @MKADAIM edittext.getText() returns Editable. Firstly, that need to be converted to String. I trimmed so that if user only enters " " in an EditText, that won't be allowed. Then I parsed the Strings to Integer. So that you can do mathematical operations on them. You can not add two Strings, But you can add two Integers. Then lastly I displayed Toast of String from result, as Toast method can't support Integer argument. – MysticMagicϡ Sep 27 '14 at 12:57
  • @MKADAIM I checked for "" to avoid blank values submission, and exception handling for NumberFormatException. If you enter "One" in EditText instead of numeric "1", then that cannot be parsed. Refer [this](http://stackoverflow.com/questions/6456219/java-checking-if-parseint-throws-exception) – MysticMagicϡ Sep 27 '14 at 13:10
0

you can not add two strings. The + operator for strings take the meaning of concatenation, and the result is a string. What do you want is probably convert the Strings in numbers and sum those, before showing the result.

try  {  
    int x = Integer.parseInt(one.getText().toString().trim());   
    int y = Integer.parseInt(two.getText().toString().trim()); 
    int z = x + y; 
    Toast.makeText(context, ""+z, 4000).show();
  }  
  catch(NumberFormatException nfe)  
  {  
    Toast.makeText(context, "Error!!!", 4000).show();  
  }  
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0
    int x = Integer.parseInt(one.getText().toString());
    int y = Integer.parseInt(two.getText().toString());
    int z = x + y;
    Toast.makeText(getBaseContext(), z, 4000).show();

Why don't use Toast.LENGTH_LONG or Toast.LENGTH_SHORT it's a go practice instead of numeric value ;)

OBuiron
  • 176
  • 6