Explore BrainMass

Java Arraylist

I am still sorting through this chapter on arrays and ran into another exercise that I do not understand. If someone could please help a struggling single mom figure this out it would be much appriciated. This program is in Java.

The following code creates a simple ArrayList:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Ex01 {
public static void main(String[] args) throws IOException {
BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
ArrayList<String> myArr = new ArrayList<String>();
Starting with this provided code, add the following functionality:
1. Add items to the ArrayList, one at a time, based on user typed input. The user will be prompted for the String to be stored in the ArrayList and then hit enter. The user will be able to continue to add items to the ArrayList until they just hit enter without typing anything.
2. Once the user does this (hits enter without typing anything), the program will display all of the elements of the ArrayList, both the index and String values, in a table. It will do this via a single loop.

Solution Preview

Solving this problem requires adding two loops to the existing
program. The first loop is used to read user input and store the
values in the ArrayList object.

boolean done = false;
while (!done) {
System.out.print("Enter a string value: ");
String value = userInput.readLine();
if (value.equals("")) ...

Solution Summary

This solution provides a detailed example of using and ArrayList with loops in Java. It also shows how to read user input from the keyboard.