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
- 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 . - 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.
departmentListand thus get an empty list, (2) if you want to add elements at the end, why not just calladd(element)?, (3) read up on the Java Code conventions - that should make you realize that names likeDepartment_Nameare discouraged. It should bedepartmentNameinstead - and since the type isDepartmentand notString(or whatever would be used to represent a name) justdepartmentmight be even better.