no_profile
no_profile

Reputation: 374

Why is my conditional statement inside a switch being ignored?

What I want is to show a toast message that says "Fill in all fields!" when the user will not write something and then they click the SAVE button.

There is no error in the code but when I run it, it will completely ignore my else condition and will proceed in adding null items to my list.

I have read somewhere in to put a break but it still does not work.

Here is a snippet code of my MainActivity.java

    case R.id.btnsave:
        if(!studLname.equals("") || !studFname.equals("") || !cboCourse.getSelectedItem().equals(0)){

            String lname = studLname.getText().toString();
            String fname = studFname.getText().toString();
            String course = cboCourse.getSelectedItem().toString();

            student.setLname(lname);
            student.setFname(fname);
            student.setCourse(course);

            Student.studentArrayList.add(student); //Global arraylist

            Log.d("test", "students:" + Student.studentArrayList);

           listView.setAdapter(studentAdapter);
            Toast.makeText(getApplicationContext(), "Item successfully added!", Toast.LENGTH_SHORT).show();
            Intent home = new Intent(AddStudentActivity.this, MainActivity.class);
            startActivity(home);
            studentAdapter.notifyDataSetChanged();
            break;
        }else{
            Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
            break;
        }

Upvotes: 0

Views: 137

Answers (6)

John Lord
John Lord

Reputation: 2185

The correct answer is a combination of everyone else's. Your "or" comparison will fire the inner check if a single box is filled in, except you aren't actually reading it correctly. You need to add getText().toString() AND change || to &&.

Upvotes: 1

sanoJ
sanoJ

Reputation: 3128

Your code has two problems,

One: You must get the text from the edittext and then check wether it's empty

Two: You must use && instead of ||, Becuase if you use || when a single field is non-empty the condition is true. Therefore in order to make sure all three values are non-empty use the && operator

Change your code as given below,

case R.id.btnsave:

String lname = studLname.getText().toString();
String fname = studFname.getText().toString();
String course = cboCourse.getSelectedItem().toString();

if(!TextUtils.isEmpty(lname) 
    && !TextUtils.isEmpty(fname)
    && !TextUtils.isEmpty(course)){ 

    student.setLname(lname);
    student.setFname(fname);
    student.setCourse(course);

    Student.studentArrayList.add(student); //Global arraylist

    Log.d("test", "students:" + Student.studentArrayList);

    listView.setAdapter(studentAdapter);
    Toast.makeText(getApplicationContext(), "Item successfully added!", Toast.LENGTH_SHORT).show();
    Intent home = new Intent(AddStudentActivity.this, MainActivity.class);
    startActivity(home);
    studentAdapter.notifyDataSetChanged();
    break;

} else {

    Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
    break;

}

Upvotes: 1

pz64_
pz64_

Reputation: 2252

Try this conditional statement

if(!studLname.getText().toString().equals("") && !studFname.getText().toString().equals("") && !cboCourse.getSelectedItem().equals(0))

You need to get Text out of the Edittext before checking it.

Upvotes: 2

Use "&"(AND) instead of "||"(OR) in the if statement. As of now, when either of the condition is true, that means only 1 field is filled and the other two are empty, then also it will enter the if condition and take null values for the NON-FILLED fields.

Upvotes: 1

dDarkLORD
dDarkLORD

Reputation: 654

Your if condition is incorrect. There should be && instead of ||.

if(!studLname.equals("") && !studFname.equals("") && !cboCourse.getSelectedItem().equals(0)){

Upvotes: 1

Abdul
Abdul

Reputation: 887

Try this. It will remove the spaces also

    if(!studLnamegetText().toString().trim().equals("") || !studFnamegetText().toString().trim().equals("") || !cboCourse.getSelectedItem()getText().toString().trim().equals(0)){

Upvotes: 2

Related Questions