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 3 of 3

Thread: help with string and counts

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with string and counts

    hi

    i need help with exercise, i have a string: "aabbbacddd".

    i need to run on that string and return evey set of letters like this:
    'a' 2; 'b' 3; 'a' 1; 'c' 1...;

    the trick part is when a letter returns again in the string after different letter.
    in the first code it returns value 1,1,1 i need 3.

    please help.

    i tried writing all sort of stuff but didnwt get the answer.

        public int countLetters(char letter, String s)
        {
            int length = s.length()-1; //using the string object method length
            int value = 0; //initialize value integer to store the list value
     
            for(int i = 0; i <= length; i++) //moving along all the characters in the string
            {
                value = 1; //first, each char is get it own value(1)
                if(i < length && s.charAt(i)==s.charAt(i+1))
                //if the char equals the next char -
                {
                    value++; //add +1 to value
                    i++; //move to the next char
                }
            }
            return value;
        }

        public static int countLetter(char c, String s)
        {
            int value = 0;
            int length = s.length()-1;
     
            for(int i = 0; i <= length; i++)
            {
                if(s.charAt(i) == c)
                {
                   value++;
                }
            }
            return value;
        }

    10x


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with string and counts

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    An interesting characteristic of chars can be used here to create a character frequency counter using an array of ints and incrementing the char's corresponding array element. You can consult an ASCII chart to verify that the ASCII value for 'a' = 97. You can also create a short program to show that:

    System.out.println( 'a' - 97 );

    Will print '0' (zero). You can use those characteristics plus an array of 26 ints to count how many characters occur in a string. Incrementing the correct element of the frequency counter will look something like:

    counter[input.charAt( i ) - 97]++;

    Good luck!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with string and counts

    hi

    10q for the quick respond.
    i found the solution, it was a bit more complicated because it's linked list exercise, and i've changed the thinking of all this.

    again 10q very much.

Similar Threads

  1. Help with Java program that counts strings and tallies results
    By IneedHelpwithJava in forum What's Wrong With My Code?
    Replies: 16
    Last Post: April 30th, 2013, 06:04 PM
  2. Arrays, code counts how many numbers in specified range
    By omarjordan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2012, 10:37 PM
  3. Replies: 1
    Last Post: November 26th, 2011, 12:13 PM
  4. Replies: 3
    Last Post: November 25th, 2011, 02:02 PM
  5. A program that counts the number of punctuation marks in a text file
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 04:03 PM

Tags for this Thread