How to make a functioning to do list in java

16,265

Solution 1

As I have mentioned in the comment, you can use an ArrayList instead of String[] to make your processing much easier.

But if you want to use the array itself, there are 3 minor issues with your code.

In your choice 1 for loop, start the loop from count,

    for (int i=count;i<MAX;i++) {
        list[i] = input.nextLine();
        if (list[i].equals("stop")) break;
        count++;
    }

In your choice 2 for loop, end the loop before reaching count,

    for (int index = 0;index < count; index++) {
        System.out.println(list[index]);                    
    }               

And move your count initialization outside the while loop.

int count = 0;

But beware, if you decide to implement removing tasks, this could get complicated and using ArrayList would become much simpler.

Solution 2

Instead of using a fixed size array of Strings, use an ArrayList of Strings. Then you can add elements to it as you go.

Make sure to

import java.util.ArrayList;

Declaration syntax is

ArrayList<String> myList = new ArrayList<String>();

Add elements to your list with the add() method:

myList.add(input.nextLine())

You don't need that inner for loop, instead break out of the while loop of input options when you've iterated through it 10 times.

To solve your problem of "stop" being in your list, check that the input is "stop", and stop, before you attempt to add to the list.

Solution 3

It is better to use ArrayList but if you still want to stick to String[] then the following program will work for you:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final int MAX = 10;
        String[] list = new String[MAX];
        int choice = 0;

        while (choice != 3) {
            System.out.println();
            System.out.println("Type 1 to add a new thing to your to do list.");
            System.out.println("Type 2 to print the to do list.");
            System.out.println("Type 3 to exit the program.");
            System.out.print("Select an option: ");
            choice = input.nextInt();
            String userEnteredItem;
            if (choice == 1) {
                System.out.println("Keep hitting enter after to do's, if you want to stop, type 'stop'.");
                for (int i=0;i<MAX;i++) {
                    userEnteredItem = input.nextLine();
                    if(!userEnteredItem.isEmpty()) {
                        list[i] = userEnteredItem;
                        if (userEnteredItem.equals("stop"))  {
                            break;
                        }
                        count++;
                    } else {
                        i--; // Do not increase index for empty item.
                    }
                }
            }
            else if (choice == 2) {
                for (int index = 0;index < count; index++) {
                    System.out.println(list[index]);
                }
            }
            else {
                input.close();
            }
        }
    }

It keeps track of user items in static int count and it also closes the scanner when you do not need it.

Share:
16,265
Nicholas Dry
Author by

Nicholas Dry

Updated on June 16, 2022

Comments

  • Nicholas Dry
    Nicholas Dry almost 2 years

    I am trying to make a functioning to do list with a limit of 10 elements, however I am having an issue with two major things in the to do list.

    The first is that after I first compile and run the program and select to add elements to the list, that functions properly, but if I add two elements and the 'stop' sentinel, when I select the next option to print the to do list, I am presented with a list, showing my two elements and then the stop sentinel along with 7 null values in the list. So the first issue I am having is to get rid of the null values, I attempted using a counter as you can see in my code however that was not proving to be effective.

    The next issue that I am having is that I am trying to make it so that you can add to the list, so once you select to add more things to the list, the new options the user writes, rewrites over them in the array and prints out the new values and not along with the old ones. I am assuming that can be done through some sort of recursion method but I am having a hard time figuring it out.

    Any help would be greatly appreciated!

    Thanks!

    import java.util.Scanner;
    
    public class ToDo {
    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
    
        final int MAX = 10;
    
        String[] list = new String[MAX];
        int choice = 0;
    
        while (choice != 3) {
    
            System.out.println();
            System.out.println("Type 1 to add a new thing to your to do list.");
            System.out.println("Type 2 to print the to do list.");
            System.out.println("Type 3 to exit the program.");
            System.out.print("Select an option: ");
            choice = input.nextInt();
            int count = 0;
    
            if (choice == 1) {
                System.out.println("Keep hitting enter after to do's, if you want to stop, type 'stop'.");
                for (int i=0;i<MAX;i++) {
                    list[i] = input.nextLine();
                    if (list[i].equals("stop")) break;
                    count++;
                }
            }
    
            if (choice == 2) {
                for (int index = 0;index < list.length; index++) {
                    System.out.println(list[index]);                    
                }               
            }
    
        }
    
    }
    }