Skip to content

Instantly share code, notes, and snippets.

@a-r-d
Created November 22, 2016 19:50
Show Gist options
  • Save a-r-d/745427b2bcdc166a8e25c28257854108 to your computer and use it in GitHub Desktop.
Save a-r-d/745427b2bcdc166a8e25c28257854108 to your computer and use it in GitHub Desktop.
An example of converting multiple lines to single array split on space chars.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ExampleMultipleStringsToList {
public static void main(String[] args) {
// read in a line
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s1 = scanner.nextLine();
System.out.print("Enter a second sentence : ");
String s2 = scanner.nextLine();
System.out.print("Enter a third sentence : ");
String s3 = scanner.nextLine();
String combined = s1 + " " + s2 + " " + s3;
String [] words = combined.split(" ");
List<String> aList = new ArrayList<>();
aList.addAll(Arrays.asList(words));
System.out.println(aList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment