Results 1 to 3 of 3
  1. #1
    allquieton is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Trying to modify an arrayList with a swtich statement. Unsuccessful so far...

    What I want to happen is to add 2+ contacts by entering "a" from the menu. Then I want to enter "c" and have it display those contacts. But it doesn't seem to remember that I changed the arrayList masterList. Any ideas?

    Java Code:
    public class ContactListApp2 {
    
        public static void main(String[] args) {
    
            boolean quit = false;
            while (quit == false) {
                System.out.println("Main Menu:");
                System.out.println("a--Add a business contact");
                System.out.println("b--Add a personal contact");
                System.out.println("c--Display all contacts");
                System.out.println("d--Quit");
                System.out.print("Make a selection: ");
    
                Scanner menuScanner = new Scanner(System.in);
                String menuSelection = menuScanner.nextLine();
                ArrayList<Personal> masterList = new ArrayList<>();
    
                switch (menuSelection) {
    
                    case "a":
                        Personal johnX = new Personal();
                        Personal.addContact(johnX);
                        masterList.add(johnX);
                        System.out.println("Contact entered.");
                        System.out.println(masterList.size());            //This prints out "1" after I add 1 or more contacts.
                        break;                                                         
                    
                    case "c":                    
                        System.out.println(masterList.size());            //This always prints out 0. Even after entering contacts.
                                                                    //But I want it to print the actual # of contacts entered. 
    
                        for (int i = 0; i < masterList.size(); i++) {
                            System.out.println((i + 1) + " " + (masterList.get(i)));    
                        }                                                                                     
                        break;
    
                    case "d":
                        quit = true;
                        System.out.println("You have quit the program.");
                        break;
                    default:
                        System.out.println("Invalid choice. ");
                        break;
                }
    
            }
    
        }
    
    }
    Here is my addContact() method:

    Java Code:
    public static Personal addContact(Personal zz) {
                
            Scanner addScanner = new Scanner(System.in);
            
            System.out.print("First Name: ");
            String y = addScanner.nextLine();
            System.out.print("Last Name: ");
            String x = addScanner.nextLine();         
            System.out.print("Birth Date: ");
            String z = addScanner.nextLine();
            
            zz.setFirst(y);
            zz.setLast(x);
            zz.setBirth(z);
            
            return zz;
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to modify an arrayList with a swtich statement. Unsuccessful so far...

    You keep re-creating the list inside of your while loop. So the earlier list gets wiped out.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    allquieton is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Trying to modify an arrayList with a swtich statement. Unsuccessful so far...

    Quote Originally Posted by jim829 View Post
    You keep re-creating the list inside of your while loop. So the earlier list gets wiped out.

    Regards,
    Jim
    Thanks for pointing me in the right direction--I'll try to rework it.

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 0
    Last Post: 09-20-2012, 04:55 PM
  3. XMLEncoder: discarding statement ArrayList.add
    By lsbill in forum New To Java
    Replies: 1
    Last Post: 04-09-2012, 09:54 PM
  4. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  5. Replies: 3
    Last Post: 01-12-2011, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •