aspose file tools*
The moose likes Java in General and the fly likes Please help on Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help on Strings" Watch "Please help on Strings" New topic
Author

Please help on Strings

nalini peddareddy
Greenhorn

Joined: Jul 18, 2005
Posts: 16
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
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
nalini peddareddy
Greenhorn

Joined: Jul 18, 2005
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 989
    
    1
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

    Joined: Jul 15, 2003
    Posts: 1078
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18139
        
      39

    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

    Joined: Jul 18, 2005
    Posts: 16
    Thanks a lot guys, i should probably go ahead with charAt() function in a for loop. I got it.
    Nor Ha
    Greenhorn

    Joined: Oct 19, 2007
    Posts: 2
    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

    Joined: Oct 13, 2005
    Posts: 36598
        
      16
    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

    Joined: Oct 27, 2005
    Posts: 19556
        
      16

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Nor Ha
    Greenhorn

    Joined: Oct 19, 2007
    Posts: 2
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Please help on Strings
     
    Similar Threads
    want to search XML files for strings
    JLIST Store and retreive data to Properties
    TreeSet vs Collection.Sort / Array.sort for Strings
    how many bytes in char
    Sorting