Results 1 to 3 of 3
  1. #1
    bh-chobo is offline Member
    Join Date
    Oct 2009
    Rep Power

    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 :


    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 = s.toLowerCase();
    		for( int i = 0; i < 26Y; i++){
    			char result = (char) ('a' + i);
    			for(int x= 0; x < s.length(); x++) {
    				if(s.charAt(x) == result){

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

    PHP Code:
    	public String toString(){	
    				for( int y = 0; y < 26; y++){			
    						for( int w = 0; w < letterData[y] ; w++){

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    Do you mean something like
    Java Code:
    StringBuilder buffer = new StringBuilder();
    for(int i =0; i < letterData.length;i++) {
    return buffer.toString();

  3. #3
    bh-chobo is offline Member
    Join Date
    Oct 2009
    Rep Power


    WOW - yeah what i meant to do - just went brain deadd.

    XML Code:
       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)));

    Came up with that - but what you proposed probably better.

    much thanks. Just shows how 2 am coding = fail for me..

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  2. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  3. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 AM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 01:42 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

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