Skip to content

Instantly share code, notes, and snippets.

@rebekah
Last active October 10, 2023 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebekah/9efcd0a9e620afab62efe7712643b506 to your computer and use it in GitHub Desktop.
Save rebekah/9efcd0a9e620afab62efe7712643b506 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class SplitWord {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(
"This program will split those characters into array items delimited by the comma.\n" +
"Enter a list of characters containing commas:"
);
String listOfCharacters = sc.next();
List<String> split = new ArrayList<String>();
String subList = "";
int index = 0;
for(int i = 0; i < listOfCharacters.length(); i++){
if(listOfCharacters.charAt(i) == ',' || i == listOfCharacters.length() - 1){
split.add(subList);
subList = "";
index++;
} else {
subList += String.valueOf(listOfCharacters.charAt(i));
}
};
String[] splitArray = new String[split.size()];
splitArray = split.toArray(splitArray);
System.out.println(Arrays.toString(splitArray));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment