Answers for "java count words in string"

3

count the number of words in a string java

public static void main(String[] args)
    { 
        //return the number of words in a string 
        
       String example = "This is a good exercise"; 
       
       int length = example.split(" ").length;
       
       System.out.println("The string is " + length + " words long.");
        
        
    }
Posted by: Guest on June-04-2020
1

how to count number of words in a string

String name = "Carmen is a fantastic play"; //arbitrary sentence
        
        int numWords = (name.split("\\s+")).length; //split string based on whitespace
                                                //split returns array - find legth of array
        
        System.out.println(numWords);
Posted by: Guest on June-06-2020
0

how to count words in string in java

String str = "I am happy and why not
  and why are you not happy and you should be";
        String [] arr = str.split(" ");
        Map<String, Integer> map = new HashMap<>();

        for (int i=0 ; i < arr.length ; i++){
                if (!map.containsKey(arr[i])){
                    map.put(arr[i],1);
                } else{
                    map.put(arr[i],map.get(arr[i])+1);
                }
        }
        for(Map.Entry<String, Integer> each : map.entrySet()){

  System.out.println(each.getKey()+" occures " + each.getValue() + " times");
        }
Posted by: Guest on January-22-2021
0

count word in string no matter the delimiter java

public static void main(String[] args)
    { 
        //Scanner object instantiation 
        Scanner dude = new Scanner(System.in); 
       
       //variable declaration 
       String string1 = ""; 
       int count = 0; 
       boolean isWord = false; 
       
       
       //user prompt and input 
       System.out.println("Enter in your string"); 
       string1 = dude.nextLine(); 
       
       int endOfLine = string1.length()-1; 
       char ch [] = string1.toCharArray(); 
       
       for (int i = 0; i < string1.length(); i++)
       { 
           if(Character.isLetter(ch[i]) && i != endOfLine)
           {//if character is letter and not end of line
               isWord = true; //it is part of a word 
           }  
           if (!Character.isLetter(ch[i]) && isWord)
           { //if character is not a letter, and previous 
             //character is a letter i.e. non-letter is 
             //preceded by character 
              count++; //add to word count 
              isWord = false; //get ready to detect new word  
           }
           if (Character.isLetter(ch[i]) && i == endOfLine)
           { //if character is letter 
             //and at end of line 
               count++; //add to word count 
               isWord = false; 
           }
           
       } 
       System.out.println("There are " +count+ " words");     
    }
Posted by: Guest on June-10-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language