Tuesday, 10 September 2013

Counts word in String array in java

Counts word in String array in java

I am writing a code to count different words from string array in java and
then storing these different words in an array and there count in another
array. If string array is like String[] appName ={"123", "abc", "Twitter",
"123", "abc","123","Twitter", "abc","abc"}; then in one array
{"123","abc","Twitter"} shoud come while in other array there count. But
there is a exception coming in line 21. Any help will be appriciable.
public class counts {
/**
* @param args
*/
public static void countWords(String[] appName){
int count =1;
int[] count1 = new int[appName.length];
String[] array= new String[12];
for(int k= 0 ;k< appName.length;k++)
{
if(array.length>0)
{
for(int l =0; l<array.length;l++)
{
if(!array[l].equals(appName[k])|| array[l]== null)
{
array[k] = appName[k];
}
}
System.out.println(array[k]);
}
}
for (int i=0; i<appName.length; i++){
String temp1 = appName[i];
//System.out.println(temp + " ");
count = 1;
for(int j=i+1; j<appName.length; j++){
String temp2= appName[j];
if(temp1.equals(temp2))
{
count++;
}
}
count1[i] = count;
System.out.println(count1[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] appName = {"123", "abc", "Twitter", "123",
"abc","123","Twitter", "abc","abc"};
countWords(appName);
}
}

No comments:

Post a Comment