Last active
October 10, 2023 07:07
-
-
Save rebekah/9efcd0a9e620afab62efe7712643b506 to your computer and use it in GitHub Desktop.
This file contains 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.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