0

I have an error when adding an element at the specified index in ArrayList I'm using loop and variable it's increasing every time when looping

  1. I tried to use a command arrayListName.add(index,thing) but this give me an error when assign second time problem ==> https://ibb.co/ZSQvywY .
  2. I tried to loop without index but this all the time assign in index [0]
public class CollegeSystem {

    public static void printOptions() {
        System.out.println("Welcome to our university!");
        System.out.println("Operations:");
        System.out.println("1- College");System.out.println("a) Number of Departments");System.out.println("b) Number of Courses");System.out.println("c) Number of Professors");System.out.println("d) Number of Students");System.out.println("e) Report");
        System.out.println("2- Department");System.out.println("a) New");System.out.println("b) Number of Courses");System.out.println("c) Number of Students");System.out.println("d) Is Full");System.out.println("e) Enroll");System.out.println("f) Report");
        System.out.println("3- Course");System.out.println("a) New");System.out.println("b) Number of Students");System.out.println("c) Assign");System.out.println("d) Is assigned");System.out.println("e) Professor Name");System.out.println("f) Is Full");System.out.println("g) Enroll");System.out.println("h) Report");
        System.out.println("4- Professor");System.out.println("a) New");System.out.println("b) Display Salary");System.out.println("c) Get Raise");System.out.println("d) Report");
        System.out.println("5- Student");System.out.println("a) New");System.out.println("b) Report");
        System.out.println("6- Quit");
    }

    public static void main(String[] args) {
        // TODO code application logic here

        printOptions() ;
        Scanner in = new Scanner(System.in) ; 
        int d = 0 , c = 0  , p = 0 , s=0 ; 
        College AinShams = new College() ; 
        while (true){
            String option = in.nextLine() ; 
            if(!"6".equals(option)) {
                if ("2a".equals(option)) { // Define new department
                    System.out.println("Department Name:");
                    String depName = in.nextLine() ; 
                    System.out.println("Department Description:");
                    String depDescripe = in.nextLine() ;
                    System.out.println("Department Max Students:");
                    int max_num = in.nextInt() ;
                    in.nextLine() ; 
                    Department Department_Name = 
                    new Department(depName,   depDescripe, max_num); 
                    List<Department> departmentList;
                   //here create a new arrayList
                   departmentList = new ArrayList<>();
                 //try to add element without index
                   //  departmentList.add(Department_Name);
                 //try to add element with index
                   departmentList.add(d, Department_Name);
                    d++ ;
                    AinShams.setDepart(departmentList);      
                }
            }   
        }   
    } 
 }

I need to add an element in every time I loop without remove old data.

1
  • There are several issues with your code: (1) you always recreate departmentList and thus get an empty list, (2) if you want to add elements at the end, why not just call add(element)?, (3) read up on the Java Code conventions - that should make you realize that names like Department_Name are discouraged. It should be departmentName instead - and since the type is Department and not String (or whatever would be used to represent a name) just department might be even better. Commented Aug 9, 2019 at 9:32

1 Answer 1

2

The reason why it's always in index 0 has nothing to do with whether you pass an index or not.

while (true){
        String option = in.nextLine() ; 
        if(!"6".equals(option)) {
            if ("2a".equals(option)) { // Define new department
                ...
                List<Department> departmentList;
               //here create a new arrayList
               departmentList = new ArrayList<>();
             //try to add element without index
               //  departmentList.add(Department_Name);
             //try to add element with index
               departmentList.add(d, Department_Name);
                d++ ;
                ....

            }
        }

Each time you add an element, you add it in a newly created List. You should create the List before your loop, and just keep the add statements in there:

List<Department> departmentList = new ArrayList<>();
while (true){
        String option = in.nextLine() ; 
        if(!"6".equals(option)) {
            if ("2a".equals(option)) { // Define new department
                ...
             //try to add element without index
               //  departmentList.add(Department_Name);
             //try to add element with index
               departmentList.add(d, Department_Name);
                d++ ;
                ....

            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.