-3
import java.util.ArrayList;
import java.util.Scanner;

/*Create a program that keeps track of specific information for Students. The information stored should be the following:
        First Name, Last Name, Major, GPA, UIN, NetID, Age, Gender,
        For this simple program we will only need to store 10 students in an ArrayList. 
        Your students should be stored in an object called Student.
        You should be able to add, display and remove Students in the ArrayList.
        You will submit 2 files for grading: Lab4.java and Student.java*/

public class Lab4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Student> newStudents = new ArrayList<Student>();
        // ArrayList<Student> newStudents = new ArrayList<Student>(10); tried this as well, but doesn't work.

        System.out.println("Welcome to the Student Interface!.");
        System.out.println("Please select a number from the options below \n");

        while (true) {
            // Give the user a list of their options
            System.out.println("1: Add a student to the list.");
            System.out.println("2: Remove a student from the list.");
            System.out.println("3: Display all students in the list.");
            System.out.println("0: Exit the student interface.");

            // Get the user input

            int userChoice = input.nextInt();
            switch (userChoice) {
            case 1:
                addStudents(newStudents);
                break;
            case 2:
                removeStudent(newStudents);
                break;
            case 3:
                displayStudent(newStudents);
                break;
            case 0:
                System.out.println("Thank you for using the student interface. See you again soon!");
                System.exit(0);
            }
        }
    }

    public static void addStudents(ArrayList<Student> newStudents) {

        Scanner input = new Scanner(System.in);
        boolean student_added = false;
        // TODO: Add a student that is specified by the user

        System.out.println("Please enter first name: ");
        String firstName = input.next();
        System.out.println("Please enter last name: ");
        String lastName = input.next();
        System.out.println("Please enter major: ");
        String Major = input.next();
        System.out.println("Please enter GPA: ");
        String GPA = input.next();
        System.out.println("Please enter UIN: ");
        String UIN = input.next();
        System.out.println("Please enter NetID: ");
        String NetID = input.next();
        System.out.println("Please enter Age: ");
        String Age = input.next();
        System.out.println("Please enter Gender: ");
        String Gender = input.next();

        for (int i = 1; i < 5; i++) { // ( I have also tried i<newStudents.size(). Didn't work.Thank you in advance!)
            newStudents.add(new Student(firstName, lastName, Major, GPA, UIN, NetID, Age, Gender));
            student_added = true;
            break;
        }

        if (student_added) {
            System.out.println("Student Added");
            System.out.println();

        } else {

            System.out.println("\n Student Interface is full!");

        }

    }

    private static void displayStudent(ArrayList<Student> newStudents) {
        // TODO Auto-generated method stub

        for (Student e : newStudents) {
            System.out.println(e);
        }
    }

    private static void removeStudent(ArrayList<Student> newStudents) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please, enter the UIN to remove the Student: ");
        String uin = input.nextLine();

        for (Student e : newStudents) {
            if (e.getUIN().equals(uin)) {
                newStudents.remove(e);
                System.out.println("Student removed");
                break;
            }

            else {
                System.out.println("Sorry, no such student with this " + uin + " " + "number exist");

            }

        }

    }

}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Fun-zin
  • 113
  • 6
  • What do you expect to happen? – Sotirios Delimanolis Jul 01 '16 at 22:32
  • 1
    Please post a [mcve] including your problem. – MasterBlaster Jul 01 '16 at 22:35
  • Thank you! I figured it out. it was suppose to print the message " student interface is full". I added, if( newStudents.size()<10) to my add method and removed the for loop. – Fun-zin Jul 01 '16 at 22:42
  • 3
    `ArrayList` does not have a max. limit. The "size" is the current number of elements. The "capacity" is not number of elements it can handle without resizing itself. – Andreas Jul 01 '16 at 22:43
  • @SotiriosDelimanolis, Sorry I am new to stackoverflow and still getting used to the "asking for help online" as well as coding. I appreciate your feedback. Thank you for your patience. – Fun-zin Jul 01 '16 at 22:53

1 Answers1

1

The Student instance is being added to the list, and there is no check for if the size of the array is larger or equal to 10. You are checking the value of the i variable, which is created when you enter the for loop.

The for loop isn't the right tool for this job in this case.

Instead, do a check for newStudents.size(), and if that does not exceed the maximum value, add the student to the list.

For example:

if (newStudents.size() <= 10) { // check the size of the array [see note 1]
    newStudents.add(new Student(firstName, lastName, Major, GPA, UIN, NetId, Age, Gender));
    System.out.println("Student added\n");
} else {
    System.out.println("\n Student interface is full!");
}

Note 1: As an aside, it'd be best if 10 was a constant at the top of the program (defined like public static const MAX_STUDENTS = 10;) to make the code more maintainable. See this question about what a magic number is.

Community
  • 1
  • 1
Justine Krejcha
  • 1,235
  • 17
  • 27