• Post Reply Bookmark Topic Watch Topic
  • New Topic

To count the occurences of all the characters in a string  RSS feed

 
jack ross
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
here is the piece of code that i have written for counting the occurences of all the characters that are there in the string .however the loop runs again for the character which has been counted.
class Arry
{
public static void main(String[] args)
{
String test = new String ("aabacbdabcvd");
int i = 0 ;
int j = 0;

for (; ((i<test.length()) && (test.charAt(i) != '?') ;i++ )
{
j = i+1;
int count = 1;
for (;(i<j && j<test.length()) ; j++)
{
if (test.charAt(i)== test.charAt(j))
{
count++;
replaceCharAt(test,j,'?');

}

}


System.out.println("The character "+ test.charAt(i) + " occured "+ count +" number of times");

}

}

public static String replaceCharAt(String s, int pos, char c)
{
StringBuffer buf = new StringBuffer( s );
buf.setCharAt( pos, c );
return buf.toString( );
}
}
the output come as

The character a occured 4 number of times
The character a occured 3 number of times
The character b occured 3 number of times
The character a occured 2 number of times
The character c occured 2 number of times
The character b occured 2 number of times
The character d occured 2 number of times
The character a occured 1 number of times
The character b occured 1 number of times
The character c occured 1 number of times
The character v occured 1 number of times
The character d occured 1 number of times

Pls help.any other solution will also help in clarification.
Thanks in advance
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya, are you using an IDE such as Eclipse or netBeans?
I would suggest stepping though your code and looking at what is happening to your variables.

Pay close attention to the string.

G
 
jack ross
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gavin for your reply .
I am not using any IDE but java editor Edit Plus so I suppose I will not be able to debug the code to see what's happening.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a bit of a shame.

Well from your code it I would guess that your issue is that yoru call to repalceCharAt dsont assign its result back to your string (test).

See if replacing your call to replaceCharAt with:


personally I would probably have converted the string to a char[] and storted the array.

G
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Gavin.
2 Things needs to be done

First:

//old
replaceCharAt(test,j,'?');
//new
test = replaceCharAt(test,j,'?');

Second :

//old
<code>
for (; ((i<test.length()) && (test.charAt(i) != '?') ;i++ )
{
j = i+1;
</code>

//new
<code>
for (;i<test.length();i++ )
{
if(test.charAt(i) == '?')
continue;
j = i+1;
</code>

Hope this helps!!
Better you install eclipse or netbeans to execute programs,it will reduce much of your hard work.
 
jack ross
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj...
Your advice helped...it worked.... thanks a lot......

I am installing an IDE now to reduce future pains.....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!