• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Please help on Strings

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am working with Strings and i have a requirement where in i have to count the number of occurences of a character in a String.I tried doing it with indexOf() method but could not do it.Could any of u help me with this?
Its really very urgent.


Thanks, in Advance
P.N.Reddy.
 
author
Posts: 23943
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried doing it with indexOf() method but could not do it.



If you post what you have done so far, I am sure that there are many here that would gladly give you a hint in the right direction.

Henry
 
nalini peddareddy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried starting like this. I have to count the number of times 'a' is occuring. But the indexOf and lastIndexOf() methods give the indexes.How do i write the logic to get the number of occurences? seems to be very basic.. but need help.

class CountChar
{
public static void main(String[] args)
{
countchar("banana");
}

static void countchar(String str){

int i= indexOf('a');
int j= lastIndexOf('a');
System.out.println("i is:"+i);
System.out.println("j is:"+j);

}
}


Regards,
P.N.Reddy.
 
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to use a "for" loop, and loop over all the characters in the String; check each one to see if it's "a", and if it is, add 1 to a counter variable. At the end, the counter has the value you want.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
You want to use a "for" loop, and loop over all the characters in the String; check each one to see if it's "a", and if it is, add 1 to a counter variable. At the end, the counter has the value you want.



If you want to show that you can think outside the box and you're using a recent enough version of Java, there are a couple other ways to do it:
  • Use String.replaceAll() and length(). What should the regexp be?
  • Use String.split() and {array}.length. What should THIS regexp be?

  • If you're really dying to use indexOf(), look at the indexOf(int, int) version and use that in, as Mr. Friedman-Hill suggested, a "for" loop. What would you use for the second int in each call? What is the terminating condition for the "for" loop?
    [ July 12, 2006: Message edited by: Ryan McGuire ]
     
    Ranch Hand
    Posts: 1078
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would go with Ernest's solution if for no other reason than it's more efficient. Thinking outside the box is great when it's a better solution, but trying to be overly clever too often results in solutions like double-checked locking.
     
    Henry Wong
    author
    Posts: 23943
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another issue is that this is probably a homework question. Using a regular expression engine is probably a no-no, regardless of how "interesting" the solution may be...

    Henry
     
    nalini peddareddy
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot guys, i should probably go ahead with charAt() function in a for loop. I got it.
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does anyone know how to modify this program so that it allows a user to input a sentence into a dialog box along with a character in that sentence, and then the program counts the number of times that character appears in the sentence?

    Type Sentence eter picket a pot of peppers
    Type Character: e

    Result
    5

    Since the sentence is not given, there needs to be an array, then the loop. I have not been able to find the missing piece of the loop that I need. I'll post more later if there someone who is interested in helping. Thanks
     
    Marshal
    Posts: 78665
    374
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch, Nor Ha.

    You have, I think, already answered your own question. Show us all what you would do to enter a sentence from a Dialog.

    If I had that assignment, I would get the String into a char[] array and traverse the array counting particular characters.
     
    Sheriff
    Posts: 22772
    130
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Campbell Ritchie:
    Welcome to the Ranch, Nor Ha.

    You have, I think, already answered your own question. Show us all what you would do to enter a sentence from a Dialog.

    If I had that assignment, I would get the String into a char[] array and traverse the array counting particular characters.


    Why create the extra array? Just use length() and charAt() from the string itself.
     
    Nor Ha
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is a program that does some of what I would like it to do, it does not allow the user to input the sentence into an array and then input a character to search for that value. It's fairly simple to find a value when it's a number, I can write a simple program so far. But I feel I am missing many crucial elements. I want the program to input a string into an array, then input the value to find, then total the number of times the value is found.


    [ October 19, 2007: Message edited by: Nor Ha ]
     
    Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic