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 :: Java program to calculate cubic capacity cc in bikes 
Java :: why use var in java 
Java :: Save SQLite returned data to an object list 
Java :: java k jump 
Java :: spring environment null pointer exception 
Java :: how to pass string between activities android 
Java :: picking a random string from string array java 
Java :: how to write 1,2,3,4.... in java 
Java :: Android equivalent of getElementById 
Java :: get beginning and end of selected text in java 
Java :: how to find the size of table in java 
Java :: is lower java 
Java :: how to load template file from resource folder in spring boot project 
Java :: how to check if parsing in integer is possible in java 
Java :: single line comment in java 
Java :: pen default Mail Application in android studio java 
Java :: how to generate a random number in libgdx 
Java :: python to java code conversion 
Java :: kotless scheduled event 
Java :: Add an element at a specified position in an ArrayList 
Java :: java search tree 
Java :: how to get individual words from a string in java 
Java :: class syntax in java 
Java :: java graph 
Java :: Calling A Class From Another Class In Java 
Java :: number of matches regex java 
Java :: java icon to image 
Java :: create a folder for multiple numbers in java 
Sql :: sql disable safe mode 
Sql :: postgresql create table default value timestamp 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =