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

    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 = 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){
    					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 = 0; y < 26; y++){			
    						for( int w = 0; w < letterData[y] ; w++){
    							System.out.print((char)('a'+y));
    						}
    			}

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Do you mean something like
    Java Code:
    .....
    StringBuilder buffer = new StringBuilder();
    for(int i =0; i < letterData.length;i++) {
      buffer.append(i+":"+letterData[i]).append("\n");
    }
    return buffer.toString();
    ?

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

    Default

    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)));
                }
            }
    
            System.out.println(output);

    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, 09:59 PM
  2. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 02:51 PM
  3. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:30 AM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 02:42 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 03: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
  •