2

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!
0

3 Answers 3

1

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!");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well the return will stop the execution, but, it will also terminate the method. So the last statement will not print anything. Also, if it would have been an int type method, then, you can not simply right 'return;'
@RishabhKumar We want the last statement to not print, as the student was indeed found. If it weren't a void method, you'd just return a value.
1

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;
}

2 Comments

Exactly! break would be a better choice over return;
break would be a better choice over return No, it is not! a break requires additional logic after the loop to find out if the loop was completed without hit or left early with hit. this additional logic is not required wit a return.
0

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!)";

1 Comment

Thank you! This worked. I used the return earlier but didn't move the error message to be outside of the loop. Thank you again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.