• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

simple question

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how to count the number of occurences of a character in a string?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter
This sounds like a homework problem - while most of the people here love to help people out we generally don't like to just do their homework for them.
Post what you have got so far and let us know where you are stuck and we'll help you out.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
A few different methods come to mind - I'm not sure which would be most efficient.
I'd bet you can figure one out by looking at the API Specification for String.
Good Luck,
-Dirk Schreckmann
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so far i've got:
char c = '@';
int index = 0;
int count = 0;

while ((index = params.indexOf(c, index)) != -1) {
count++;
}
System.out.println(count);
but i'm getting stuck in the loop and don't know what to do with it.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
You have an infinite loop. Your index never changes so (index = params.indexOf(c, index)) always produces the position of the desired character which is never -1 (unless the character isn't in the String, which would produce a -1) - and so the infinite loop.
Also, your code doesn't do what your thread topic is asking about. Your code doesn't count anything. It can be modified to find the position of your desired character by incrementing index and not count - but I'm not sure that is what you are trying to do.
My powers of guessing are dwindling - it's too late... must sleep... I probably don't have the wherewithal to spell my name correctly...
Good Luck,
-Fred Flinstone
...or make up a good joke.
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone suggest a way of counting characters then?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one way!


//Kaspar
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic