It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Start of a Crypto program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Start of a Crypto program" Watch "Start of a Crypto program" New topic
Author

Start of a Crypto program

Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Welcome to JavaRanch

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.
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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:

Example:
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

substring returns a new String object, so it doesn't modify the current one.

What you can do is keep an index that you increase with 24 until all is gone. In pseudo code:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
Alrighty, here's what I have so far (with comments).


Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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...


Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

This initializes 'block' as a String array with exactly 2 indices, block[0] and block[1]. You are trying to assign a String to block[2] and that correctly results in an ArrayIncexOutOfBoundsException.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Sine you don't know how many elements will be there after the split, you should use a collection class like ArrayList instead of an array...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Jim Hester wrote:

Right, my bad. nextBlock should be the minimum of startBlock + endBlock and line.length().

Also, why is endBlock 23? You want blocks of 24 characters, and the end index of substring is exclusive, so 24 is the way to go.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

You'll get a StringIndexOutOfBoundsException.
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
This is what I ended up doing to initialize the array, and it works ok...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Wouldn't empty Strings ("") be better?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You are right about needing to check the blockEnd value, Rob. I had indeed forgotten about the Exception.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Start of a Crypto program