find frequency of each word in a string in java
Java
//import the necessary packages if needed
import java.util.*;
import java.math.*;
@SuppressWarnings("unchecked")//Do not delete this line
public class CountOfWords
{
public static void main (String[] args) {
BigInteger a = new BigInteger("1");
Map<String, BigInteger> countByWords = new HashMap<String, BigInteger>();
TreeMap<String, BigInteger> sorted = new TreeMap<String, BigInteger>();
Scanner sc = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>();
while (sc.hasNext())
{
strings.add(sc.next().toLowerCase());
}
BigInteger num= new BigInteger("0");
for(String s: strings) {
num=num.add(new BigInteger("1"));
s=s.trim();
s=s.replace(",","");
s=s.replace(";","");
s=s.replace(":","");
s=s.replace(".","");
s=s.replace("?","");
s=s.replace("!","");
BigInteger count = countByWords.get(s);
if (count != null) {
countByWords.put(s, count=count.add(new BigInteger("1")));
} else {
countByWords.put(s, a);
}
}
sorted.putAll(countByWords);
System.out.println("Number of words "+num);
System.out.println("Words with the count");
for (String name: sorted.keySet()){
String key = name.toString();
String value = sorted.get(name).toString();
System.out.println(key + ": " + value);
}
System.out.println(" ");
}
}
Also in Java:
- Title
- remove spaces at beginning and end of string java
- Category
- Java
- Title
- java change boolean to opposite
- Category
- Java
- Title
- getcolor deprecated
- Category
- Java
- Title
- check java version cmd
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- arraylist to array java
- Category
- Java
- Title
- java string to boolean
- Category
- Java
- Title
- android switch on change
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- iterate through hashmap java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- index 1 out of bound for length 1 java
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- arrays.fill java
- Category
- Java
- Title
- java random number between 100 and 999
- Category
- Java
- Title
- context error in android studio
- Category
- Java
- Title
- inheritance in java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- java program to display characters from a to z using loop
- Category
- Java
- Title
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- write a java program to check whether given number is binary or not
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- edit a txt file using java
- Category
- Java
- Title
- how to access private vairable in java method
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- java prev permutation
- Category
- Java
- Title
- google map set default zoom android studio
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- math min max java
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- date format in java
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- graal.js pass javascript array to java function
- Category
- Java
- Title
- java unit test an api
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- pascal's triangle java 2d array
- Category
- Java
- Title
- check if user has internet connection in kotlin
- Category
- Java
- Title
- split arraylist items java
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- how to get all the names of the files in a folder in java?
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- make recycler view non scrollable
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- Java array hashcode
- Category
- Java
- Title
- java try catch
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- funzione random in java
- Category
- Java
- Title
- two pointer approach java
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- spigot respawn player location
- Category
- Java
- Title
- get random number from enum in java
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- add in list java
- Category
- Java
- Title
- checking if a given integer is a prime number java
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- capcitor FERR_CLEARTEXT_NOT_PERMITTED
- Category
- Java
- Title
- uppercase java
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- validate isbn number java
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- java insert into arraylist
- Category
- Java
- Title
- log4j with spring boot restful services
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java parse json
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- java recursion
- Category
- Java
- Title
- length of string java
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- deserialize date java
- Category
- Java
- Title
- java array check duplicates
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- charat(0).touppercase() java
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- how to do sex java
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- spark write partitionby
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- error message pushes button down
- Category
- Java