Java String Tokens

  • + 0 comments

    I just "improved" (it doesnt use magic number, and I check if its empty the string one time, but I use more memory) another solution I found on this discussion section. the og solution: https://programmingoneonone.com/hackerrank-java-string-tokens-problem-solution.html

    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            final String splitRegEx = "[^a-zA-Z]";
            List<String> listStrSplited = new LinkedList<>();
            
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            scan.close();
            
            String [] draftlistStrSplited = s.split(splitRegEx);
            
            for(String splittedStr : draftlistStrSplited){
                if(!splittedStr.isEmpty()){
                    listStrSplited.add(splittedStr);
                }
            }
            System.out.println(listStrSplited.size());
            for(String splittedStr : listStrSplited){
                System.out.println(splittedStr);
            }
        }
    }