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

Thread: Need help with concatenizing char? to string?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with concatenizing char? to string?

    I'm stuck, and having a bump in my program, here is a little snippet of the contructor just ot give you an idea.

    To Sum it up - I am running the letterCount to take in a string set by the client. It will take that string, and tally how many occurences of each letter occurs within that string inside an array of 26 (26 letter in alphabet).

    0 = A
    1 = B
    2 = C

    etc etc.

    If the client passed a string of "AAAB", then the array would have the value 3 in index 0.

    So The main issue lies within the 2nd code i posted, my toString method. I am required to have the toString method return a string representation of my letterCount object.

    For example, using my example above, index[0] will have value of 3 - because we stored 3 letter 'a's previously, and index[1] would have had 1.

    I need the toString method.. to somehow.. take those values and output it back out again? and return it as a string? I've been only able to list them back out as println's and i'm quite stumped on how to put it back together as a single string..

    The expected string should produce :

    [aaab]


    So my question is - I kind of need pointers or help on how I can, instead of printing values one after another, just return a full single string representations.



    PHP Code:
            private int[] letterData

        public 
    LetterCount(String data){
                
    letterData = new int[26];
                
    sizeCount 0;
            
            
    String s data;
            
    s.toLowerCase();
        
            for( 
    int i 026Yi++){
                
    char result = (char) ('a' i);
                
                for(
    int x0s.length(); x++) {
                    if(
    s.charAt(x) == result){
                        
    letterData[i]++;
                        
    sizeCount++;
                    }
                }
            } 


    I am having issues with this method, I know I'm not doing it right..



    PHP Code:
        public String toString(){    
                
                    for( 
    int y 026y++){            
                            for( 
    int w 0letterData[y] ; w++){
                                
    System.out.print((char)('a'+y));
                            }
                } 


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need help with concatenizing char? to string?

    How about...

            final StringBuilder stringBuilder = new StringBuilder();
     
            for (int y = 0; y < 26; y++) {
                for (int w = 0; w < letterData[y]; w++) {
                    stringBuilder.append((char) ('a' + y));
                }
            }
     
            return stringBuilder.toString();

    ooooooooooooooor...

            String output = "";
     
            for (int y = 0; y < 26; y++) {
                for (int w = 0; w < letterData[y]; w++) {
                    output = output.concat(Character.toString((char) ('a' + y)));
                }
            }
     
            return output;

    I'd use the StringBuilder

    Enjoy!

    // Json
    Last edited by Json; October 16th, 2009 at 03:56 AM.

  3. The Following User Says Thank You to Json For This Useful Post:

    bh-chobo (October 16th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with concatenizing char? to string?

    I'll give those a try - i seriously just went BRAIN DEAD on how to actually concatenate it all together.

    Much thanks!

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Need help with concatenizing char? to string?

    If thats everything, using "Thread Tools" could you please mark this thread as solved.

    Thanks,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 08:33 AM
  2. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM
  3. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM
  4. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM