Java String Tokens

Sort by

recency

|

1775 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine().trim();
        if (s.isEmpty()) {
            System.out.println(0);
            return;
        }
        String[] words = s.split("[^A-Za-z]+");
        System.out.println(words.length);
        for (String w : words) System.out.println(w);
    }
    

    }

  • + 0 comments

    Solved

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        // Write your code here.
        String[] arrayTemp = s.split("[ !,?._'@]+");
        StringBuffer strResult = new StringBuffer();
        int count = 0;
        for(int i = 0; i < arrayTemp.length; i++){
            if(arrayTemp[i].matches("[A-Za-z]+")){
                count++;
                strResult.append(arrayTemp[i]+"\n");
            }
        }
        System.out.println(count+"\n"+strResult);    
    
        scan.close();
    }
    
  • + 1 comment

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        // Write your code here.
        String st = s.trim();
        if(st.length() == 0)
         System.out.println("0");
        else {
        String[] result = st.split("[ !,?._'@]+");
        System.out.println(result.length);
        for(String str : result)
         System.out.println(str);
        }
        scan.close();
    }
    

    }

  • + 1 comment

    Hello !!

    I'm realy close to solve that problem but test no 9, he didn't give up. i fail that test, the single one. could you help me with an hint? My solution is this:

            Scanner scan = new Scanner(System.in);
        String s = scan.nextLine().trim();
        String[] tokens = s.split("[ !,?._'@]+");
        System.out.println(tokens.length);
        for (int i = 0; i < tokens.length; ++i) {
            System.out.println(tokens[i]);
        }
        scan.close();
    
  • + 0 comments

    well i tried my best unable to pass few test cases. Anyone could help me out??

      public static void myStringTokenizer(String a){
        
            
            
            if(1<=a.length() && a.length() <=(4*100000)){
              //regex
                String regex = "[!,?._@]+";
                String str = a.replaceAll(regex, "");
                str = str.replaceAll("['    ]+", " ");
                String[] s = str.split(" ");
                // System.out.println(Arrays.toString(s));
                System.out.println(s.length);
                for(String temp: s){
                    System.out.println(temp);
                }  
            }
            
        }