Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java word count

package com.company;
import java.util.*;

public class Main{
    public static int countWords(String s)
    {
        int count=0;

        char ch[]= new char[s.length()];
        for(int i=0;i<s.length();i++)
        {
            ch[i]= s.charAt(i);
            if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
                count++;
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Give word:");
        String word = in.nextLine();
        System.out.println(countWords(word) + " words.");
    }
}
Comment

java-word-count

import java.util.HashMap;
2
import java.util.Locale;
3
import java.util.Map;
4
5
public class WordCount {
6
7
    public WordCount() {
8
9
    }
10
11
    public Map<String, Integer> phrase(String text) {
12
        Map<String, Integer> map = new HashMap<>();
13
        String[] textParts = text.toLowerCase()
14
                .trim()
15
                .replaceAll("[.-_:;?!@^&%$§=]", "")
16
                .replaceAll("'([a-zA-Z0-9]+)'", "$1")
17
                .replaceAll("^[,s]+","")
18
                .split("[,s]+");
19
20
        for (String textPart : textParts) {
21
22
            if (map.containsKey(textPart)) {
23
                int counter = map.get(textPart) + 1;
24
                map.put(textPart, counter);
25
            } else {
26
                map.put(textPart, 1);
27
            }
28
29
        }
30
31
        return map;
32
    }
33
34
}
35
Comment

PREVIOUS NEXT
Code Example
Java :: how to close android app programmatically 
Java :: retrofit dependencies in android studio kotlin 
Java :: unable to find bundled java version. flutter 
Java :: java random seed 
Java :: How to convert timestamp to time in java 
Java :: java jcheckbox get value 
Java :: java regex ip 
Java :: create date java 
Java :: generate a random phone number in java 
Java :: system.out.println shortcut 
Java :: kotlin enable and disable parents view children 
Java :: hashmap sort by value descending 
Java :: localdate add months in java 
Java :: java string array 
Java :: get first character of string java 
Java :: entre clavier java 
Java :: java split for multiple characters 
Java :: java int array 
Java :: space in java 
Java :: display an image java 
Java :: java program to print 1 to 100 using for loop 
Java :: java char to charsequence 
Java :: change javahome cmd 
Java :: How to efficiently find three largest numbers of array, in Java? 
Java :: java random primary key 
Java :: java random usage 
Java :: comment in java 
Java :: select max value from list java 
Java :: How to use Runnable interface to develop multi-threaded Java programs? 
Java :: get time android 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =