Jose Beltran
Jose Beltran

Reputation: 61

Edit and Update ArrayList in Java

I am trying to update myArray list by allowing the user to change their first and last name while keeping other information in the arraylist the same.

the code follows

public void editStudentID(int findStudentId) {

        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() != findStudentId) {
                continue;
            }
            System.out.println("Found a profile containing information for " + findStudentId + ":");
            System.out.println("What would you like to change in your profile?");
            System.out.println("1.First Name");
            System.out.println("2.Last Name");
            int decision = scanner.nextInt();
            switch (decision) {
                case 1:
                    System.out.println("Enter a new first name to continue");
                    String newFirstName = scanner.next();//need to find a way to update this in my arraylist
                    break;

                case 2:
                    System.out.println("Enter a new last name to continue");
                    String newLastName = scanner.next();//this as well
                    break;
            }
            return;
        }
        System.out.println(" Id not found ");

    }

this is my Student class where I only wrote final for only my id and dob to not be changed by the user

public class Student {
    private final int id;
    private String firstName;
    private String lastName;
    private final String dob;

    public Student(int id, String firstName, String lastName, String dob) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getDob() {
        return dob;
    }

    public static Student createStudentID(int id, String firstName, String lastName, String dob) {
        return new Student(id, firstName, lastName, dob);
    }

}

Upvotes: 1

Views: 439

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

First, you need to make you Student class mutable, but supplying a couple of "setters" which will allow you to change the first and last name properties, for example

public class Student {

    //...

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

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

}

Then in you editStudentID method, you simply update the required record, for example

public void editStudentID(int findStudentId) {

    for (int i = 0; i < students.size(); i++) {
        if (students.get(i).getId() != findStudentId) {
            continue;
        }
        System.out.println("Found a profile containing information for " + findStudentId + ":");
        System.out.println("What would you like to change in your profile?");
        System.out.println("1.First Name");
        System.out.println("2.Last Name");
        int decision = scanner.nextInt();
        switch (decision) {
            case 1:
                System.out.println("Enter a new first name to continue");
                String newFirstName = scanner.next();//need to find a way to update this in my arraylist
                students.get(i).setFirstName(newFirstName);
                break;

            case 2:
                System.out.println("Enter a new last name to continue");
                String newLastName = scanner.next();//this as well
                students.get(i).setLastName(newFirstName);
                break;
        }
        return;
    }
    System.out.println(" Id not found ");

}

Upvotes: 1

Related Questions