Search code examples
javaarraylistprintingsublistempty-list

Printing sublist of an ArrayList (To-Do List based on user input) results in empty ArrayList


A very simple to-do list that asks for input and then prints out the list in the form of an ArrayList divided into sections (sublists)(I have really bad vision, so I have to use large fonts, and when the list gets too long, the problem is, that the end of the list runs off the page. As much as I can use the home/end buttons to quickly view the page, it's not an optimal situation. I'd rather break the ArrayList into sublists, and print out the sublists, one on each line, like the following:

Here is today's to-do list: [Wake up, Walk the dog,Eat breakfast] [Make bed,Sweep out back, Study Java]

import java.util.Scanner; import java.util.ArrayList;

/**
 * @author Troy
 *
 */
public class HelloWorld {


    public static void main(String[] args) {
        // I chose an ArrayList because the size does not have to be predetermined.
        ArrayList<String> to_do = new<String>ArrayList();
        System.out.println("What would you like to add to your to-do list?");
        Scanner user_input = new Scanner(System.in);
        //While the user_input still has entries, perform the following:
        while (user_input.hasNextLine()) {
            //Add next entry in the to-do list(user_input) to the ArrayList
            String input = user_input.nextLine();
            //If input = remove, remove the last item in the to_do list.(ArrayList)
            if ("remove".equals(input)) {
                if (to_do.size() > 0) {
                to_do.remove(to_do.size() -1);
            }}
            /**If the user types in "exit", when prompted for the next item in their
             * to_do list, close user_input, and print out... 
             */
            if ("exit".equals(input)) {
                user_input.close();
                System.out.println("Your to-do list is complete!");

                ArrayList<String> sect1 = new ArrayList<String>(to_do.subList(0, to_do.size()));                
                if (to_do.size() <= 5) {
                    System.out.println(sect1 + "\n");
                    break;
                }
                ArrayList<String> sect2 = new ArrayList<String>(to_do.subList(6, to_do.size()));
                if (to_do.size() > 5 && to_do.size() <=10) {
                    System.out.println(sect1 + "\n" + sect2);
                    break;
            }
            //If input does NOT equal "remove", add user_input to the to_do list.
                if (!"remove".equals(input)) {
                    to_do.add(input);
                }

            System.out.println("\n");
            /**Print the ArrayList called "to_do" split into sections AFTER writing, 
             * "Here is today's to-do list:"
             *  */
            System.out.println("Here is today's to-do list: " + "\n");
            if (to_do.size() <= 5) {
                System.out.println(sect1 + "\n");
            }
            if (to_do.size() > 5 && to_do.size() <=10) {
                System.out.println(sect1 + "\n" + sect2);
            }

        }
    }
}}

Solution

  • As the other poster already stated, the problem with your code is the incorrect nesting of the if blocks. This causes your to_do.add to be inside the if ("exit".equals(input)) block, so your list remains empty. I recommend using an IDE and letting it re-indent (format) your code, then this problem will become far more apparent.

    But aside from that, there is another problem in your code: your sect1 takes subList(0, to_do.size()) which is your entire list. This will cause it to print the entire list on one line which is what you are seeing. I suggest you instead use a loop and divide the list in equal chunks that way. Since subList already returns a list, you also don't have to wrap it in another ArrayList, and you can print it directly.

    So I corrected your code to this:

    import java.util.Scanner;
    import java.util.List;
    import java.util.ArrayList;
    
    /**
     * @author Troy
     */
    public class HelloWorld {
        public static void main(String[] args) {
            // I chose an ArrayList because the size does not have to be predetermined.
            List<String> toDo = new ArrayList<String>();
            System.out.println("What would you like to add to your to-do list?");
            Scanner userInput = new Scanner(System.in);
    
            // While the userInput still has entries, perform the following:
            while (userInput.hasNextLine()) {
                // Get the next line entered by the user
                String input = userInput.nextLine();
    
                //If input is "remove", remove the last item in the toDo list. (ArrayList)
                if ("remove".equals(input)) {
                    if (toDo.size() > 0) {
                        toDo.remove(toDo.size() -1);
                    }
                }
                /*
                 * If the user types in "exit", when prompted for the next item in their
                 * toDo list, close userInput, and print out... 
                 */
                else if ("exit".equals(input)) {
                    userInput.close();
                    System.out.println("Your to-do list is complete!");
                    System.out.println("Here is today's to-do list: ");
    
                    final int perLine = 3;
                    int i = 0;
                    while(i < toDo.size()) {
                        // Print from the start of our current chunk (i)
                        //  to the end (i+3), or to the size of the list if our last chunk is smaller than "perLine".
                        System.out.println(
                            toDo.subList(i, Math.min(toDo.size(), i+perLine))
                        );
                        i+=perLine;
                    }
    
                    break;
                }
                /*
                 * If input is neither "remove" nor "exit", add input to the list
                 */
                else {
                    toDo.add(input);
                }
            }
        }
    }
    

    I also changed some variables to be camelCase instead of snake_case, as is convention in Java.