0
    ```
    System.out.println(menu);
            boolean quit = false;
            int selection;
            do {
            // next user input (of integer type) will be stored in the variable selection
            selection = scanner.nextInt();
            
            switch (selection) {
            case 1:
                System.out.println("Please enter a new record as John Michael West Doe, 574 
    Pole ave, St. Peter, MO, 63303, 3142752000");
                // method to split at each comma 
                // method to add that input to a Person Object
                    
                while (scanner.hasNextLine()) {

                    String str = scanner.nextLine();
                    String[] inputs = new String[6];
                    inputs = str.split(",");
                    
                    for (int i = 0; i < inputs.length; i++) {
                        System.out.println(inputs[3]);
                    }
                }

I'm trying to figure out why my inputs array isn't storing the values of the string (input from scanner) split after each comma.

I keep receiving a:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at PhoneBook.main(PhoneBook.java:52)

I'm trying to do this with just arrays, no array lists.

When I print System.out.println(inputs[i]) - it prints everything split by comma, so I've been messing with my code for days and scouring the web trying to figure out what's going on. Any advice would be much appreciated! I'm going through a coding bootcamp currently and am super excited for things to start clicking slowly. LOL.

  • Without spending a lot of time looking at it, the first issue I see is the fact that you have a dangling new line in the `Scanner` buffer left over from `selection = scanner.nextInt();`. It's a good idea to use `scanner.nextLine()` in after it, in this case, to consume the newline left in the buffer – MadProgrammer Aug 23 '20 at 22:36
  • @MadProgrammer Wow. I had no idea I needed to absorb the new line. That made the world of difference. Thank you. – Evan Chapman Aug 23 '20 at 23:21
  • @AlexRudenko Thank you! I didn't know that the .nextInt() created a buffer that needed to be absorbed. – Evan Chapman Aug 23 '20 at 23:22

0 Answers0