0

This is my .java that includes my main method:

package studentapp;

public class Studentapp {
    public static ArrayList<Student> students = new <Student> ArrayList();
    public static ArrayList snumbers = new ArrayList();
    public static Scanner user = new Scanner(System.in);
    public static int sncount = 0;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
        students.add(new Student("Kevin", "Swim", "AAS", 3.5));
        students.add(new Student("Kyle", "Swim", "AAS", 2.8));
        students.add(new Student("Bob", "Smith", "AAS", 2.8));
        sncount = 1;
        createArraynum();
        createMenu();
}


private static void addastudent(){
    System.out.println("First Name: " );
    String fname = user.next();
    System.out.println("Last Name: ");
    String lname = user.next();
    System.out.println("Major: ");
    String major = user.next();
    System.out.println("GPA: ");
    double gpa = user.nextDouble();
    Student a = new Student (fname,lname,major,gpa);
    students.add(a);
    a.sNumber = 1234500 + sncount;
    snumbers.add(a.sNumber + sncount);
    sncount ++;
    createMenu();

    }
private static void findaStudent(int input){
    if (snumbers.contains(input)) {
        for(Student i : students){
            if(i.sNumber == input)
            {System.out.println(i.sNumber +" "  + i.firstName + " " + i.lastName +
                     " " + i.major + " gpa: " + i.gPA );

            }
            }
    } else {
       System.out.println("Sorry, The Student with that sNumber does not exist!");
} 
    createMenu();
}


private static void deleteaStudent(int input){

    if (snumbers.contains(input)) {
        for(Student i : students){
            if(i.sNumber == input){
                int number = students.indexOf(i);
                students.remove(number);
                System.out.println(i.sNumber +" "+ i.firstName+" "+i.lastName + " has been deleted!");
            }
            else { System.out.println(" ");}
            }
    } else {
       System.out.println("Sorry, We cannot find a student with that number!");

    }
}
private static void displayStudents(){
    for(Student i : students){
        System.out.println(i.sNumber +" "  + i.firstName + " " + i.lastName +
                     " " + i.major + " gpa: " + i.gPA );
    }
    createMenu();
}
private static void displayNumStudent(){
    System.out.println(students.size());
    createMenu();
}
private static void exit(){
    System.out.println("Thank you, Goodbye");
}
private static void createMenu(){

        System.out.println("1. Add a Student");
        System.out.println("2. Find a Student");
        System.out.println("3. Delete a Student");
        System.out.println("4. Display all Students");
        System.out.println("5. Display the total number of students");
        System.out.println("6. Exit");
        System.out.println("Enter your Selection: ");

        int count = user.nextInt();
        switch (count){
            case 1: addastudent();
                 break;
            case 2: System.out.println("Find student with sNumber S");
                    int number = user.nextInt();
                    findaStudent(number);
                 break;
            case 3: System.out.println("Delete student with sNumber S");
                    int number1 = user.nextInt();
                    deleteaStudent(number1);
                 break;
            case 4:  displayStudents();
                 break;
            case 5:  displayNumStudent();
                 break;
            case 6:  exit();
                 break;
            default : System.out.println("Please Make a valid Selection");
                      createMenu();

        }
}
private static void createArraynum(){
    for(Student i : students){
        i.sNumber = 1234500;
        i.sNumber = i.sNumber + sncount;
        snumbers.add(i.sNumber);
        sncount ++;
    }
}

}

and this is my Students Class.

public class Student {
String firstName;
String lastName;
int sNumber = 1234500;
String major;
double gPA;
int count;


public Student(String fName, String lName, String maj, double gpa){
    firstName = fName;
    lastName = lName;
    major = maj;
    gPA = gpa;


}
public String getFirstName(){
    return this.firstName;
}
public String getLastName(){
    return this.lastName;  
}
public void setFirstName(String name){
    this.firstName = name;

}
public void setLastName(String name){
    this.lastName = name;

}
public int getSNumber(){
    return this.sNumber;    
}
public String getMajor(){
    return this.major;

}
public void setMajor(String smajor){
    this.major = smajor;

}
public double getGpa(){
    return this.gPA;

}
public void setGpa(double num){
    this.gPA = num;

}
@Override
public String toString(){
    return firstName;

}

}

Everything seems to work the way i want it to. except when i try and call the deleteaStudent method. then i get errors if I try to remove the student from the arrayList students.

error looks like this:

    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at studentapp.Studentapp.deleteaStudent(Studentapp.java:68)
    at studentapp.Studentapp.createMenu(Studentapp.java:115)
    at studentapp.Studentapp.main(Studentapp.java:28)
Java Result: 1

How can I get this to work properly?

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
Kevin Swim
  • 17
  • 5

1 Answers1

0

You can't remove a student from the list while iterating over it with the enhanced for loop. You can use an explicit iterator instead.

Iterator<Student> iter = students.iterator();
while (iter.hasNext() {
    Student student = iter.next();
    if (someCondition)
        iter.remove();
}

And in your method :

private static void deleteaStudent(int input){

    if (snumbers.contains(input)) {
        Iterator<Student> iter = students.iterator();
        while (iter.hasNext() {
            Student i = iter.next ();
            if(i.sNumber == input){
                iter.remove ();
                System.out.println(i.sNumber +" "+ i.firstName+" "+i.lastName + " has been deleted!");
            }
            else {
                System.out.println(" ");
            }
        }
    } else {
       System.out.println("Sorry, We cannot find a student with that number!");

    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768