1

For a personal project of mine of which I already have written the code for the console version of it, I need an ArrayList of strings. The strings of the ArrayList are added via console input. This is how I did that:

 ArrayList<String> input = new ArrayList<>();
 Scanner sc = new Scanner(System.in);
 while (sc.hasNextLine()) {
        String lineNew = sc.nextLine();
        if (lineNew.isEmpty()) {
            break;
        }
        input.add(lineNew);
    }

So I basically add each line the Scanner reads to the ArrayList.

So what I did in my JavaFX version was that I created a textarea of what I will get the input from. After printing the class of the output i saw that the gettext() method of a textarea returns a String and not an ArrayList, which is already kind of strange on its own since the input contained enters. Anyways, is there a method which returns an ArrayList of strings instead of just a string with a textarea, or a different kind of text field, or do I need to make substrings out of the long string and put it into an ArrayList, or is there something else I could do.

Thanks in advance.

1
  • 1
    Split the string on a common delimiter, such as the new line character Commented Jul 6, 2018 at 0:07

2 Answers 2

2

The getText() correctly, and predicably, will return the entire text entered into a TextArea. In order to get an ArrayList<String>, you would need to do the conversion yourself.

Thankfully, it is incredibly simple to do so. Strings have a split() method that allows you to create an array by splitting the string based on a delimeter. In your case, you would use the new line character, or \n. Combine that with the Arrays class and you get a nice and easy ArrayList of each line in the TextArea:

ArrayList<String> textLines = 
    new ArrayList<>(Arrays.asList(textArea.getText().split("\n")));

Here is a simple MCVE to demonstrate:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Just a simple interface
        VBox root = new VBox(5);
        TextArea textArea = new TextArea() {{
            setWrapText(true);
        }};
        Button btnGetArray = new Button("Print Array");

        // Set the action of the button to print the result of creating the ArrayList
        btnGetArray.setOnAction(event -> {
            System.out.println(getStringArray(textArea.getText()));

        });

        // Show the interface        
        root.getChildren().addAll(textArea, btnGetArray);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private ArrayList<String> getStringArray(String string) {
        // This method builds an array list by taking the full String and splitting it
        // at each new line (\n) character.
        return new ArrayList<>(Arrays.asList(
                string.split("\n")
        ));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Zephyr! I already had this idea in mind but wondered if there was not already a build in one.
According to the JavaDoc for String, you can get a char[] array directly from the String using toCharArray(), but anything beyond that is up to you. docs.oracle.com/javase/7/docs/api/java/lang/String.html
0

TextArea.getParagraphs returns a list containing a String for every line. To copy the content to a ArrayList, simply use the constructor of ArrayList:

List<String> list = new ArrayList<>(textArea.getParagraphs());

Or to only get the Strings before the first empty one:

List<String> paragraphs = textArea.getParagraphs();
int endIndex = paragraphs.indexOf("");
List<String> list = new ArrayList<>(endIndex >= 0 ? paragraphs.subList(0, endIndex) : paragraphs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.