1

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);
    }

}

1
  • 1
    Since the firstName and lastName are private, you'll need setters. Commented Sep 1, 2020 at 5:34

1 Answer 1

1

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 ");

}
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.