Results 1 to 3 of 3
- 10-16-2009, 09:47 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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)); } }
- 10-16-2009, 09:54 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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();
- 10-16-2009, 10:28 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 08:59 PM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Assigning a string value to a char
By coffeebean in forum New To JavaReplies: 4Last Post: 06-15-2008, 06:30 AM -
Char to String in java
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 01:42 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM
Bookmarks