简体   繁体   中英

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

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

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);
while (!textLine.isEmpty())

userInput永远不会为null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM