• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help on Strings

 
nalini peddareddy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • 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 ]
     
    Ken Blair
    Ranch Hand
    Posts: 1078
    • Mark post as helpful
    • send pies
    • 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
    Marshal
    Pie
    Posts: 21022
    78
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
    • Quote
    • Report post to moderator
    Thanks a lot guys, i should probably go ahead with charAt() function in a for loop. I got it.
     
    Nor Ha
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • 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
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • 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.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20514
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • 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
    • 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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic