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);
            } 
        } 
    } 
}









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
Get rows count and get columns names
COUNT ROWS AND GET COLUMN NAMES OF A TABLE   In this program , we count...++;                 }       System.out.println("Number of Rows present in table : "+count...);       }     } } In this program , we count the number of data rows stored
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
Java count occurrence of number from array
Java count occurrence of number from array Here we have created an example that will count the occurrence of numbers and find the number which has... and accepted the numbers through the console. We have restricted to enter the number above
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
Count number of nodes
(struct node *p ) { int count=0; while (p != NULL) { count ++; p = p->link; } return(count); } void main() { int n; int x; struct node *start = NULL ; printf("Enter number of nodes to be created
PHP Array Count Occurences
Array Search In PHP, array_search() function performs a search  for a value in an array, as with in_array(). If the value is found then the index position of that key (which could be string or number) will be returned
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 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
How can we know the count/number of elements of an array?
How can we know the count/number of elements of an array?  How can we know the count/number of elements of an array
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
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
Count numbers of spaces and number of words from the input string
Count numbers of spaces and number of words from the input string Java In this section, you will learn how to count the number of spaces and words from... the number of spaces. Now, in order to count the number of words, we have splitted
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... of Prime Number program in Java: package Tutorial; public class PrimeNumber
Java file line count
Java file line count In the section, you will learn how to count the number of lines from the given file. Description of code: Java has provide various...++; } System.out.println("Number of Line: " + count
prime number - Java Beginners
prime number  this project is to determine if a phone number is a prime number. if the number is a prime number then print a message to the effect. if the number is not a prime number then print the prime factors of the 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
count asterisk in a grid - Java Beginners
count asterisk in a grid  I need to write a program that uses a recursive method to count the number of asterisk in a square grid
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
Java file number of lines
Java file number of lines In this section, you will learn how to count the number of lines from the given file. Description of code: Java has provide... of the file. We have used counter here to detect the number of lines occurred
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
Hibernate Count
In this section you will learn different way to count number of records in a table
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
PHP Array Count Values
PHP Array Count Values  Sometimes  we need to count the number... array_count_values() function to count the number of variables.  General...($array); echo"Counting the number of values are:"; print_r(array
count in java - Java Beginners
, count of frmale is 20. what's code in java to select count from database where...count in java  Hello javamania.... i want to ask something like... is 20. i have JFrame Form(jTextField,jLabel,jButton), i want to get count
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
how to print a new line after a certain number of prints
how to print a new line after a certain number of prints  How do I make this program print the inputs the user gives 5 per line? So in other words I want the program to print 5 input numbers per line. this is my code so far
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
PHP array count
The PHP array_count_values() function is used to count the elements in the array. It returns the number of counting with values in associative array form. In returned array, key is the array's values and values are the number
Number Convert - Java Beginners
passString(int number){ int num, q ; if (number < 10 ){ displayOutput(a[number...]) ; } if ( number > 19 ){ num = number % 10 ; if ( num == 0 ){ q = number / 10 ; displayOutput (d[q-2]) ; } else{ q = number / 10 ; displayOutput(a[num
print a rectangle - Java Beginners
print a rectangle  how do I print a rectangleof stars in java using...()); System.out.print("The number of triangles you want to output in each row...("The number of rows of triangles you want to output : "); int noOfRows
PHP Array Count
;; $b=array(0=>0,3=>4); print_r($b);ADS_TO_REPLACE_5 $count=count($b...;Elements of the \$a is:</b><br/>"; print_r($a); $count...=array(0=>0,3=>4); print_r($b);ADS_TO_REPLACE_10 $count=sizeof($b
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
letter count problem - Java Beginners
letter count problem  i have a problem in my java coding to count two characters of a string. eg to count the letter "ou" in the string "How do you feel today?". The answer should be 5. but i still have error compiling
Print Form - Java Beginners
Print Form  Hello Sir I have Created Admission Form when user fills data and submit that data to access database,then when i Click on PRINT Button I want to get Print of that forms Contents,How I can Do it with JAVA SWING,plz
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
print
print  How to print JFrame All Componant?   Please visit the following link: http://www.roseindia.net/java/example/java/swing/Print.shtml
Java count files in a directory
Java count files in a directory In this section, you will learn how to count... directory. But we have to count the number of files so we have initialized...()) { count++; } } System.out.println("Number of files: " + count
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
Simplest way to print an array in Java
Simplest way to print an array in Java  Simplest way to print an array in Java
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
print rectangle pattern in java
print rectangle pattern in java  * * * * * * * how to generate this pattern in java??   Hi friend try this code may this will helpful for you public class PrintRectangle { public static void main
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
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
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

Ads