-1

...output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out-of-bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source) at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source) at java.base/java.util.Objects.checkIndex(Unknown Source) at java.base/java.util.ArrayList.get(Unknown Source) at HelloWorld.main(HelloWorld.java:27) ...output

My program is a to-do list and is as follows:

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

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

    /** A very simple to-do list that asks for input and then prints out the  list 
     * in the form of an ArrayList: eg:
     Here is today's to-do list:
     [Wake up, Walk the dog,Eat breakfast, Make the bed]
      */
    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
            to_do.add(user_input.nextLine());
            System.out.println("\n");
            /**Print out the ArrayList(to_do) 4 elements at a time. */
            for (int i = 0; i < 5; i++ ) {
                System.out.println("Your list for today is: " + "\n" + to_do.get(i));

            }

            }
    }
}

...Java......................................................................

I get the above error message as soon as I write the to_do.get(i) at the very end of the program.

Another bonus question is how to end the while loop without affecting the output of the final ArrayList?

Troy
  • 101
  • 9

1 Answers1

2

Your for-loop tries to access values from the list that do no exist yet. This is why you get an IndexOutOfBoundsException.

If you replace the test expression in your for-loop with something that checks the size of the list instead of hard-coding it to 5, it will work:

for (int i = 0; i < to_do.size(); i++)

You can end the input cycle by using a break. Check the input before adding it to the list by assigning it to a local variable first. If it is for example a blank space (as a signal that the user does not have any further input) execute the break from the while-loop.

String input = user_input.nextLine();
if (" ".equals(input) || i == 4) {
    break;
}
to_do.add(input);

If you wanted to print only 4 entries at a time it would look something like this:

System.out.println("Your list for today is: " + "\n");
for (int page = 0; page <= (to_do.size() / 4); page++) {
    for (int offset = 0; offset + page * 4 < to_do.size() && offset < 4; offset++) {
            System.out.println(to_do.get(page * 4 + offset));
        }
        user_input.nextLine();
    }
}
KUSH42
  • 552
  • 2
  • 13
  • Thank you. I'm no longer getting that error message. The only problem now, is that the for loop is not working to print out 4 elements of the ArrayList at one time. (I can get it to print out the whole list at once, but when the list is too long, it runs off the page and is no longer visible without scrolling. I'd rather print out only 4 elements at one time and then repeat the process with the 2nd set of 4 elements, etc. Any thoughts or help you could give would be greatly appreciated! – Troy Sep 30 '18 at 21:28
  • 1
    By adding another for loop that prints four entries at a time and then waits for user input before printing the next four, until all entries have been printed. I amended my answer with an example. – KUSH42 Sep 30 '18 at 22:15
  • Thank you KUSH42! I'll give that a shot. – Troy Oct 01 '18 at 01:31