I need to take a String of any given length, and convert it to a series of 24 character blocks, padding the length, if necessary, with blanks.
Not sure at all how to do this. I don't even have a good psuedo code algorithm yet.
My thought is to convert the string to a character array and then treat it as a queue, popping the first 24 characters into a string, then resetting the counter and starting over. Not entirely sure how to add the blank chars onto the end, though. Any help would be appreciated.
How about the substring method?
What about putting the whole String into a StringBuilder, taking and excising 24-character substrings, then when the length falls below 24, appending space characters at the end?
StringBuilder is quite easy to use. Only if you have the index of something, and look for the next index of the same, you need to start searching from one position along the String, or you get the same index repeatedly. You can join several methods together in a StringBuilder by addiing . after each method call.
ok, substring sounds promising. I'm a little confused as to how it works though. Here's what I WANT to have happen.
I read in a file that has several lines of strings:
This is a test string with more than 24 characters
This is another line of characters with more than 24 characters.
This line has less.
I want to create a string array for each line, for example:
My question is: what happens to the string that you do a substring from? Is the substring subtracted from the original string? If not, what's a good way to increment so that you get the rest of the characters in the string?
It was giving me an error about a null pointer for a while, but I figured out how to initialize the array, but I'm still running into an error:
It REALLY doesn't like this line:
It points to that line when it encounters the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
Not entirely sure what I'm doing wrong. I threw some printlns in there to show that the numbers are just as they should be. The nested while loop appears to run ok, but it dies before being able to write the arrays to the screen.. I dunno what I'm doing wrong at this point...
You can work out the number of substrings by using / 24 and % 24; if the latter is not 0, then you add 1 to the count.
If you have a 245-character String that will give length / 24 of 10 and length % 24 of 5, so you need an 11-member array with indices from 0 to 10 inclusive.
No idea what will happen if you manage to split the two halves of a code point of 32 bits.
Do we actually need a minimum value for the nextBlock index? What happens if you call substring(4, 99) on a String like "Campbell Ritchie" which has 16 characters in? Don't you simply get "bell Ritchie"? I can't remember, but the API should have the answer in.