Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What String methods should I use for this?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What String methods should I use for this?

    I have loaded a file into a String, now I need to determine how many letter 'a' there are in the file and print the amount, etc. How can I accomplish this? I need a for loop to scan through but not sure how to implement it. Could it by like:

    for(int i = 0; i < String.lenght(); i++){

    here is the confusion

    }

    maybe add some if statements? Help. Thanks in advance.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What String methods should I use for this?

    You will need to compare each char in the String to 'a' using an in statement, if it is an 'a' increment a count.

    Or you could use a regular expression.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What String methods should I use for this?

    Which of the String class methods look like they can be useful? You need to get at each character in the String to be able to test it. What methods return a char?

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What String methods should I use for this?

    you can try this,this code should work:

    [full solution deleted by KevinWorkman]
    Last edited by KevinWorkman; August 3rd, 2011 at 09:21 AM.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What String methods should I use for this?

    migongotar, don't post full code solutions. Spoon-feeding is NOT helping. Read this: http://www.javaprogrammingforums.com...n-feeding.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: What String methods should I use for this?

    You need to loop through the string, and each time round the loop, find the character at the position indicated by your loop counter, and if that character is an 'a', increment another counter.

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: What String methods should I use for this?

    Another alternative, if you wanted to count numbers of other characters too, is to use one of the java.util.Arrays.sort() methods on the character array alluded to in some of the other comments. Then you could produce counts of every character with a single pass through the array. If you had an array index type of for loop, you wouldn't need to increment a count, you could just remember the index the current character started at and take the difference of the indexes when the character next changes.
    Last edited by Sean4u; August 3rd, 2011 at 02:31 PM. Reason: missing definite article

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  4. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  5. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM