0

Have a problem with this code!

I want to check the editText values, if it is null or not... But it gets stuck at the if segment, doesnt mather if it is a value in the editText or not. If there is a value in the editText string it should go further and calculate the values.

Second problem I have is the toast, it doesnt show the text in the string variable, it just prints the string link.

private EditText fp;
        private EditText fC;
        private EditText drive;
        private TextView totalcost;
    public void CalcButton(View button) {
        // Converting strings to float and check if each is NULL (empty)
        if (!(fp.getText().equals(null)) || (fC.getText().equals(null)) || (drive.getText().equals(null)))
        {
            Toast.makeText(getApplicationContext(), "@string/toast", Toast.LENGTH_LONG).show();

        }else {
            String n1 = fp.getText().toString();
            float no1 = Float.parseFloat(n1);
            String n2 = fC.getText().toString();
            float no2 = Float.parseFloat(n2);
            String n3 = drive.getText().toString();
            float no3 = Float.parseFloat(n3);
            // Calculates the floats
            float calc = no1 * no2 * no3;
            // Converting and prints out the result
            String sum = Float.toString(calc);

                totalcost.setText(sum);
            }
perror
  • 7,071
  • 16
  • 58
  • 85

5 Answers5

2

You should not do it this way, do this instead:

if (!fp.getText().toString().equals("")) {

} 
M0CH1R0N
  • 756
  • 9
  • 19
1

To problem with Toast - use this:

  Toast.makeText(getApplicationContext(), R.string.toast, Toast.LENGTH_LONG).show();
PetrS
  • 1,110
  • 14
  • 38
1

Try to use TextUtils.isEmpty() instead, it checks for null and 0-length String.

On your if statement it should look like:

if (!TextUtils.isEmpty(fp.getText().toString())) {
    // Code
} 

And on your Toast, change "@string/toast" to R.string.toast or getApplicationContext().getString(R.string.toast);

The code should look like:

// Converting strings to float and check if each is NULL (empty)
if (! (TextUtils.isEmpty(fp.getText().toString()) || 
        (TextUtils.isEmpty(fC.getText().toString())) || 
        (TextUtils.isEmpty(drive.getText().toString()))))
{
    Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.toast), Toast.LENGTH_LONG).show();
}

More on the getString() method here.

EDIT: I've seen that your code also was missing a pair of parenthesis ( ). So your "not" was only applying to the first test.

Something like:

!(test1) || test2 || test3

Instead of

!((test1) || (test2) || (test3))
Mauker
  • 11,237
  • 7
  • 58
  • 76
0

To check if EditText is empty you do editText.getText().toString().equals("")

So, Your if-statement will look like this:

if (!(fp.getText().toString().equals("")) || 
     (fC.getText().toString().equals("")) ||
     (drive.getText().toString().equals("")))

And your Toast would be like this:

Toast.makeText(getApplicationContext(), R.string.toast, Toast.LENGTH_LONG).show();
iTurki
  • 16,292
  • 20
  • 87
  • 132
0
function isNull(int resourceId, boolean getError){
    EditText editText= (EditText) findViewById(resourceId);
    String strEditText = String.valueOf(editText.getText());

    if(TextUtils.isEmpty(strEditText)) {
        if(getError) editText.setError("this is null!");
        return true;
    }else{
        return false;
    }
}

May be this block gives you a few clues about yours

about If Conditions; In your 'IF conditions', parentheses seems less than the count it is necessary. Try to add one more after (!)

I guess it should be like this; for Negative

if (!(
    (String.valueOf(fp.getText()).equals("")) || 
    (String.valueOf(fC.getText()).equals("")) || 
    (String.valueOf(drive.getText()).equals(""))
))

for Positive

if (
    (String.valueOf(fp.getText()).equals("")) || 
    (String.valueOf(fC.getText()).equals("")) || 
    (String.valueOf(drive.getText()).equals(""))
)
Kaloglu
  • 1,651
  • 1
  • 20
  • 31
  • Have tried different advicing code examples... but same result. Seems like the If segment doesnt recognize when the strings have values, Seems like the IF sees them empty doesnt matter what. – Henrik Granström Aug 09 '15 at 13:07