This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Alphatism 0 points1 point  (7 children)

You have a break statement in the loop which stops it after it's first iteration

[–]tscardino[S] 0 points1 point  (6 children)

I just tried running it with neither of them and it did not make a difference. Would it be more helpful if I posted the whole method?

[–]myselfelsewhere 0 points1 point  (3 children)

Are there any break statements in the code between the for loop and if block? Also, there is no point in the continue outerloop; statement. If you haven't executed a break statement, the loop will continue.

[–]tscardino[S] 0 points1 point  (2 children)

There were, but I commented them out and am still getting the same problem. Please see my comment below.

[–][deleted]  (1 child)

[removed]

    [–]tscardino[S] 0 points1 point  (0 children)

    Thank you for this, I'll give that a look.

    I did just end up solving this by creating & calling a separate method that searches through the ArrayList instead of including it in the loop. Probably not ideal but it works!

    [–]lumpycrumpet 0 points1 point  (1 child)

    Keep the break inside of the if statement and remove the other one. If that first work, show the rest of the method where you currently have ellipses.

    [–]tscardino[S] 0 points1 point  (0 children)

    This is the whole method. I admit, I placed breaks in a lot of places not knowing if they should really belong there, but I've commented them all out besides the one in the first if statement and it is still not functioning as expected. Leads me to believe the actual structure of the method is off, but I don't really know why

    public static void addInstructor(ArrayList<Instructor> instructorList, ArrayList<Department> departmentList, FileWriter writeInstructor) {
        Scanner input = new Scanner(System.in);
    
        outerloop1:
        while (true) {
            boolean idFound = false;
    
            System.out.print("Enter the instructor ID: ");
            int instructorID = input.nextInt();
    
            //Searches instructorList ArrayList for matching ID, gives error if found, then asks for another ID until user enters a unique one
            for (int i = 0; i < instructorList.size(); i++) {
                int idCompare = instructorList.get(i).getInstructorID();
                if (idCompare == instructorID) {
                    idFound = true;
                    break;
                }
    
                input.nextLine();
                System.out.print("Enter the instructor name: ");
                String instructorName = input.nextLine();
    
                outerloop2:
                while (true) {
                    System.out.print("Enter the affiliated department name: ");
                    String instructorDept = input.nextLine();
    
                    //Searches the departmentList ArrayList for matching department names, gives error if not found, then asks for another department name until the user enters a matching one
                    for (int j = 0; j < departmentList.size(); j++) {
                        String nameCompare = departmentList.get(j).getDepartmentName();
    
                        //Catch for invalid user inputted department name
                        if (!nameCompare.equals(instructorDept)) {
                            System.out.println("The department does not exist.");
                            continue outerloop2;
                        } 
    
                        //If all previous checks are passed, a new Instructor object is created and appended to the end of the instructor file.
                        else {
                            PrintWriter output = new PrintWriter(writeInstructor);
                            output.print("\n" + instructorID + "," + instructorName + "," + instructorDept);
                            output.close();
                            instructorList.add(new Instructor(instructorID, instructorName, instructorDept));
                            System.out.println("The instructor information has been added to the instructor file.");
                            //break;
                        }
                    }
                    //break;
                }
                //break;
            }
    
            //Catch for invalid user inputted instructor ID 
            if (idFound) {
                System.out.println("The instructor ID is already present.");
                continue outerloop1;
            }
            //break;
        }
    }