Created
November 22, 2016 19:50
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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