Count number of occurences and print names alphabetically in Java

Count number of occurences and print names alphabetically in Java

I have this code:

public class Names { public static void main(String[] args) { String[] s = {"bob", "tom", "jim", "tom", "tom", "tim"}; printCount(s); }

public static void printCount(String[] sa) { //code } }

Need to write the code for the printCount() method for the code to count the number of occurences of each word and print in alphabetical order to produce this output:

{bob=1, jim=1, tim=1, tom=3}

View Answers

May 16, 2011 at 1:11 PM

import java.util.*;

public class CountWordOccurrence {
        public static void main(String[] args){

        String[] st = {"bob", "tom", "jim", "tom", "tom", "tim"};
        HashMap<String, Integer> map = new HashMap<String, Integer>();
            String str="";
            for(int i=0;i<st.length;i++){
                str+=st[i]+" ";
            }
            str = str.toLowerCase(); 
                int count = -1;
                for (int i = 0; i < str.length(); i++) { 
                   if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) { 
                            if (i - count > 1) { 
                            if (Character.isLetter(str.charAt(i))) 
                                i++;
                            String word = str.substring(count + 1, i);
                            if (map.containsKey(word)) { 
                            map.put(word, map.get(word) + 1);
                            }
                            else { 
                            map.put(word, 1);
                            } 
                        } 
                        count = i;
                    } 
                } 
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.addAll(map.values());
        Collections.sort(list, Collections.reverseOrder());
        int last = -1;
        for (Integer i : list) { 
            if (last == i) 
                continue;
            last = i;
            for (String s : map.keySet()) { 
                if (map.get(s) == i) 
                    System.out.println(s + ":" + i);
            } 
        } 
    } 
}

Ads









Related Tutorials/Questions & Answers:
Count number of occurences and print names alphabetically in Java
Count number of occurences and print names alphabetically in Java  I have this code: public class Names { public static void main(String[] args... for the printCount() method for the code to count the number of occurences of each
Sorting Country names alphabetically
Sorting Country names alphabetically  Hello, I have a list of country names in an array. I need a jsp code which will sort me the country names in an alphaberical order. It would be more useful when I get the coding using
Advertisements
Count number of "*"
Count number of "*"  I have this code to count the number of * from...:"); String text = bf.readLine(); int count = 0; for (int i = 0; i...); if (c=='*' ) { count
Count number of characters in a column.
Count number of characters in a column.  I just need you suggestions. Am from Biology Back ground. I just need to find out number of characters... to count characters in 1st, 2nd, 3rd columns seperatly and also print flase if i
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
print square of any number
print square of any number  using c++ language, write aprogram to print the square of any number entered by the user
write a java program to print marklist of n students. input Register number, name and marks of three subjects.
write a java program to print marklist of "n " students. input Register number, name and marks of three subjects.  write a java program to print marklist of "n " students. input Register number, name and marks of three subjects
Java count vowels
Java count vowels In this section you will learn how to count the number... and then you will get the number of vowels count. Description of code : In this code.... to console. Example  : A Code to count the number of vowel in a string
print the sum of even number from 1 to 100
print the sum of even number from 1 to 100  how to print the sum of even number from 1 to 100 using for loops? Thanks
Java Word Count - Word Count Example in Java
Java Word Count - Word Count Example in Java  ... to count the number of lines, number of words and number of characters... some strings and program will count the number of characters and number of words
print selected checkbox names in array without form tag
print selected checkbox names in array without form tag  Hi everyone ... I have problem in my program. I have hashmap i.e. collection , my... seleced checked checkbox names, when i click on button but without using FORM tag
Prime Number program in Java
Prime Number program in Java will print the prime numbers between 1 to any given number. Prime Number is a number that is not divisible by any number other... automatically prints Prime Number starting from 1 to 50. Example of Prime Number
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  import java.io.File; import java.io.FileNotFoundException; import java.util....()); System.out.println("The number of unique words: "+uniqueValues.size
Hibernate Count
In this section you will learn different way to count number of records in a table
Count the character in java
Count the character in java  Write a java program to count....   Count characters by implementing thread import java.util.*; class CountCharacters { public static void count(final String str){ Runnable
How to print this in java?
How to print pattern in Java?  How to print a particular pattern in Java...;  How to print this in java
Retrieve a list of words from a website and show a word count plus a specified number of most frequently occurring words
. print: the total number of words processed, the number of unique words, the N... int N = 25; //the number of word/frequency pairs to print //word pattern...; Integer count; // number of occurrences WordPair(String word
Counting specific word occurences in a file
Counting specific word occurences in a file  Hello I have a log file from the proxy which consists all the browsing history with date,time,url,ip... name and need to count how many times those sites(url's) are being visited
Alphabetically sorting order
Alphabetically sorting order  Write a java program that takes a list of words from the command line and prints out the arguments in an alphabetically sorted order   Hi Friend, Try the following code: import
how to find [count the number of integers whose value is less than the average value of the integers]
how to find [count the number of integers whose value is less than the average... amount to the screen integers from an operator at a terminal, and count the number... program is to display the average integer value and the count of integers less
Ask java count
Ask java count  Good morning, I have a case where there are tables... | Java 1 | 10 | | b002 | beginner java | 5 | | b003 | advanced java book | 26 | | b004 | MySQL 1
How to format number in Java?
How to format number in Java?  Hi, What is the best way to format a number in Java? How to format number in Java? Thanks   Hi, To format number in Java you can use the class java.text.NumberFormat. Here is simple
how to count words in string using java
++; } System.out.println("Number of words are: "+count); } } Thanks   Hello... count=0; String arr[]=st.split(" "); System.out.println("Number...how to count words in string using java  how to count words in string
count characters
count characters  i have to print the total number of char 'A' in all... sabah sarawak terengganu the output must be count the total number of char... main(String[] args) { int count=0; Scanner input=new Scanner
Magic number Java Program
Magic number Java Program  write a program that guesses what number the user is thinking of. Below is a sample transcript: Think of a number between... the number, skip the next one, print the next one, skip the next one, etc
Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount.
Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount.  Modify the sales tax program to accept an arbitrary number of prices, total them, calculate
getting random number in java
getting random number in java  getting random number in java Hi... and scaling the random number in my Java Random number eample. but for some reason... to generate the random number in Java Thanks in Advance
Java Print Dialog
Java Print Dialog  Using java.awt.print.PrinterJob and javax.print.attribute.PrintRequestAttributeSet. I call .printDialog(ps) and the standard print dialog is displayed with options preset to my chosen attributes. Now I can
Simplest way to print an array in Java
Simplest way to print an array in Java  Simplest way to print an array in Java
NSArray Count Example
NSArray Count Example In the example, we are going to count the number of elements in an array in objective c programming. In Objective C language, Count is a primitive instance method that returns the number of objects available
PHP SQL Number of Rows
. To understand how to count the number of rows in a table, we have created a sql_num_rows.php..._rows($res);   print("$number_of_rows rows found"... PHP SQL Number of Rows      

Ads