Results 1 to 3 of 3
  1. #1
    AwesomeGuy is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Count Reoccurring Characters

    Hi guys I'm a little stuck with this so any suggestions would be great.

    My goal is to analyze a character arraylist such as xxxxxxaaaa and then turn it into x6a4. This means basically for every time a character repeats it counts it and then prints the character and how many times in a row it repeats.

    Here's how I think I can do however I don't feel like this is the best way to accomplish it. If anyone could provide suggestions to do it more efficiently that would be great.

    I'll have a for loop which will continue until it has reached the size of the initial arraylist. I have a counter variable to keep track of the number of times the character appears so far.

    It then has an if statement to compare the current element in the array list to the previous one. If they match it will increase the counter variable if not it will print the previous character and the total value of the counter variable. It will then set the counter variable back to zero.


    Any suggestions on how to improve? Thanks in advance.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    What happened when you tried that? Did it work? Did it have any problems?

    I'm not trying to be a smartass. Writing a simple version of that program should take no longer than a half hour, but waiting on responses (which might be wrong anyway) could take a few days.

    I'd say your best bet is to write a version of your program, then post it with any specific questions or problems you had. You're much more likely to get advice on improving your algorithm once you actually have the code written.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AwesomeGuy View Post
    Hi guys I'm a little stuck with this so any suggestions would be great.

    My goal is to analyze a character arraylist such as xxxxxxaaaa and then turn it into x6a4. This means basically for every time a character repeats it counts it and then prints the character and how many times in a row it repeats.

    Here's how I think I can do however I don't feel like this is the best way to accomplish it. If anyone could provide suggestions to do it more efficiently that would be great.

    I'll have a for loop which will continue until it has reached the size of the initial arraylist. I have a counter variable to keep track of the number of times the character appears so far.

    It then has an if statement to compare the current element in the array list to the previous one. If they match it will increase the counter variable if not it will print the previous character and the total value of the counter variable. It will then set the counter variable back to zero.


    Any suggestions on how to improve? Thanks in advance.
    You don't have to keep track of a 'count' variable because you have to keep track of an index variable for the String already; suppose i points to a character in the String; also suppose for a variable j > i the character at position j differs from the character at position i. So all characters in the range [i, j) are equal and there are j-i characters in that range. Your count equals j-i. For the next chunk of characters set i to j and repeat until you've checked the entire String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Count Characters
    By java_student88 in forum Advanced Java
    Replies: 9
    Last Post: 03-28-2011, 12:00 PM
  2. Count down
    By tj_wolf2 in forum New To Java
    Replies: 8
    Last Post: 03-14-2011, 05:07 PM
  3. How to use a timer to count up or down.
    By caryr in forum AWT / Swing
    Replies: 5
    Last Post: 06-28-2009, 08:33 PM
  4. Count the characters in an input box.
    By TheRocket in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 01:25 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •