How do I allow a user to input strings into an array until they hit enter with no text input?

user2825293

I'm really hoping someone can help me out here. I'm still fairly new to Java and I have spent hours trying to figure out how to do this. I have a loop to prompt the user to input text (string) into an arraylist however, I cannot figure out how to end the loop and display their input (I want this to happen when they press 'enter' with a blank text field. Here is what I have - Thank you in advance!!

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>();

        myArr.add("Zero");
        myArr.add("One");
        myArr.add("Two");
        myArr.add("Three");

        do {
            System.out.println("Enter a line of text to add to the array: ");

            String textLine = userInput.readLine();
            myArr.add(textLine);
        } while (userInput != null);

        for (int x = 0; x < myArr.size(); ++x)
            System.out.println("position " + x + " contains the text: "
                    + myArr.get(x));
    }
}
Bernhard Barker

There's a difference between a null variable and an empty string. A null variable is a variable that's not referencing anything. An empty string is a String of length 0 sitting somewhere in memory, which variables can reference.

readLine only returns null if the end of the stream is reached (see the docs). For standard input this won't happen while the program is running.

More importantly, you're checking whether the BufferedReader will be null, not the string it reads it (which can never happen).

And the problem with changing the code as is to just check whether the string is empty instead is that it will still be added to the ArrayList (which is not a particularly big deal in this case - it can just be removed, but under other circumstances the string would be processed, in which case it would be a problem if it were empty).

There are a few work-arounds for this:

They hack-y way, just remove the last element afterwards:

// declare string here so it's accessible in the while loop condition
String textLine = null;
do
{
    System.out.println("Enter a line of text to add to the array: ");
    textLine = userInput.readLine();
    myArr.add(textLine);
}
while (!textLine.isEmpty());
myArr.remove(myArr.size()-1);

The assignment-in-the-while-loop-condition way:

String textLine = null;
System.out.println("Enter a line of text to add to the array: ");
while (!(textLine = userInput.readLine()).isEmpty())
    myArr.add(textLine);
    System.out.println("Enter a line of text to add to the array: ");
} ;

The do-it-twice way:

System.out.println("Enter a line of text to add to the array: ");
String textLine = userInput.readLine();
while (!textLine.isEmpty())
    myArr.add(textLine);
    System.out.println("Enter a line of text to add to the array: ");
    textLine = userInput.readLine();
};

The break-in-the-middle-of-everything way (generally not advised - avoiding break is usually preferred):

String textLine = null;
do
{
    System.out.println("Enter a line of text to add to the array: ");
    textLine = userInput.readLine();
    if (!textLine.isEmpty())
        break;
    myArr.add(textLine);
}
while (true);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I read a line of input from a user, until an EOF is hit, in GNU Prolog?

How do I ask for user input until the input is a number?

How Can I get input from user until user enter the right input in java?

How do I ask for user input until the user stops?

How do I stop user input until I need it?

How do I style text input by a user?

read user input into array until user enter specific entry

How do I calculate years until graduation for a user's input?

How do I enter data from user input into a 2D array in Java?

How do i store user input in an array?

How do I allow a text input to a TensorFlow model?

How to input strings into array until input string matches criteria?

How can I get input integers from user until he press enter by using eof?

How do i pick out specific strings from a user input?

How do I limit user input to two possible strings?

How to not allow the user to progress until the correct input is entered?

input submit on enter hit

How do I retrieve user input from a textbox after users hit the submit button?

How do i repeat asking for input after my user pressed enter on my input prompt

How do I count the number of occurrences of the invalid input when the user enter the wrong input?

how do i make a command where pressing ENTER or not Submiting an input cause it to repeat until it gets the right input in batch?

How to populate an array with multiple strings input by the user?

ARRAYFORMULA Enter data if relevant but allow user input if not

React Native: How do I generate a box after user input with the text from user input?

How do I only allow the user to enter sensible height and weight values in Edit Text? Java

How do I check if each UITextField in an Array has a user input?

How do I print the user's input (Array) in java?

How do i clear this array by user input in php/html?

How do i use user input for a string array that is in a while loop?