Josh Low
Josh Low

Reputation: 23

Test a condition in a while loop then exit while loop in Java

I'm new to Java and I'm trying to understand how to nest an if statement inside a for loop and have it exit the for loop once the if statement is executed. I have an array, the for loop goes through the array to see if an ID exists, if it does its supposed to delete it, if it doesn't exist then it should print an error message. What is happening is the condition is test in the nested if statement in the while loop and printing the error message 3 times. I would like it to only print the error message once.

In my main method I have

remove("3");
remove("3");

on the first one it should just remove that ID and print that it was rem, the second one it should only print the error message once. This is a project for school and requires no input from a user. I'm just trying to understand how to make this work without printing out repeat error messages

public static void remove(String studentID) 
{

    for (int i = 0; i < thestudents.size(); i++) 
    {

        Student temp = thestudents.get(i);

        if (temp.getStudentID()==(Integer.parseInt(studentID))) 
        {
            thestudents.remove(i);
            System.out.println("Student " + temp.getFirstName() + " was removed");
        }
        else
        {
            System.out.println("Student with ID " + studentID + " Was not found!");
        }
    }
}

The result:

Student with ID 3 Was not found!
Student with ID 3 Was not found!
Student Jack was removed
Student with ID 3 Was not found!
Student with ID 3 Was not found!
Student with ID 3 Was not found!
Student with ID 3 Was not found!
Student with ID 3 Was not found!

Expectation:

Student Jack was removed
Student with ID 3 Was not found!

Upvotes: 2

Views: 756

Answers (3)

Timothy Truckle
Timothy Truckle

Reputation: 15622

Just adding a break will remove the output after the match, but the output before the match will remain.

I guess you want to get rid of all of the false negative outputs.

Therefore you have to move the negative output (which is the content of the else block) after the loop (deleting the else line) and make sure this code gets not executed when the ID was found.

The best way to do this is to add a return as the last statement in the if block.

for (int i = 0; i < thestudents.size(); i++) 
{
    Student temp = thestudents.get(i);
    if (temp.getStudentID()==(Integer.parseInt(studentID))) 
    {
        thestudents.remove(i);
        System.out.println("Student " + temp.getFirstName() + " was removed");
        return; // leaving the method whe ID found
    }
}
// is only executed when ID not found
System.out.println("Student with ID " + studentID + " Was not found!)";

Upvotes: 0

Kim Clarence Penaflor
Kim Clarence Penaflor

Reputation: 150

Just add a break inside the if statement. If that if statement is true, then the loop will terminate.

if (temp.getStudentID()==(Integer.parseInt(studentID))) {
    hestudents.remove(i);
    System.out.println("Student " + temp.getFirstName() + " was removed");
    break;
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 311188

You could use a break statement to terminate the loop, or better yet, a return statement to completely terminate the method once you find the appropriate item:

public static void remove(String studentID) 
{

    for (int i = 0; i < thestudents.size(); i++) 
    {

        Student temp = thestudents.get(i);

        if (temp.getStudentID()==(Integer.parseInt(studentID))) 
        {
            thestudents.remove(i);
            System.out.println("Student " + temp.getFirstName() + " was removed");
            return;
        }
    }

    // If we get here it means we haven't returned, so the student wasn't found
    System.out.println("Student with ID " + studentID + " Was not found!");
}

Upvotes: 1

Related Questions