In Java, How would you put the following string in an arrayList to have all the
ID: 643988 • Letter: I
Question
In Java,
How would you put the following string in an arrayList to have all the words after two capitalized letters at its own index ( I have a much larger list of addresses than just these two) in an arrayList? I've tried the pattern thing but didn't have that much luck. The following output should show the arrayList as {Linda Dumas 3393 Pennsylvania Avenue Portland TX,Huazhang Hanlon 3428 Main Road Miami NH}
This is what I had last as code:
String testString = "Linda Dumas 3393 Pennsylvania Avenue Portland TX Huazhang Hanlon 3428 Main Road Miami NH ";
Pattern pattern = Pattern.compile("[A-Z]{2} ");
String[] split = pattern.split(testString);
System.out.println("split.length = " + split.length);
for(String element : split){
System.out.println("element = " + element);
}
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("[A-Z]{2}").matcher(testString);
while (m.find()) {
allMatches.add(m.group());
}
System.out.println(allMatches);
Explanation / Answer
Answer:
your array list variable in the main method.
example
if You want to see elements in array-list as a single string, add this code to above one .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.