Reading a substitution table from a file
Posted: Wed Feb 11, 2004 10:45 pm
Hey all, I'm getting back into Java for one more semester, let's see if I can make it through.
My problem is, I've been tasked with reading in a substitution file and doing encryption stuff with it.
The file's format is:
A X
B N
C Y
D A
etc.
where the first letter is the decrypted letter and the second letter is its encrypted counterpart. I'm having problems with getting the file read into an array, which would then be passed to a constructor of a different class to do more stuff with it (per assignment specifications).
Heres what I have so far, using the file C:\substitutiontable.txt:
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
//located in the main method
{
fileLocation=(getInputString("Enter the location of the substitution table: "));
char[][] subTable= fileReaderToArray(fileLocation);
for (int i=1; i < subTable.length; i++)
{
for (int j=0;j < subTable.length; j++)
{
System.out.println(i + " "+ j+" " + subTable[ i ] [j]);
}
}
}
//end main method after doing some more stuff
//helper method for substitution operation, reads subs. table into a 2D array of type char
private static char[][] fileReaderToArray(String fileInput) throws FileNotFoundException,IOException
{
FileReader subTable=new FileReader(fileInput);
char[][] cbuf=new char[3][26];
while (subTable.ready()){
for (int i=1; i < cbuf.length; i++) {
for (int j=0; j < cbuf[ i ] .length;j++){
cbuf [ i ] [j]=(char)subTable.read(); }}}
subTable.close();
return cbuf;
}
</pre><HR></BLOCKQUOTE>
I know I'm missing something, i just dont know what. It doesnt help that I'm a little rusty on my Java skills, having skipped a semester since my last course in it. *sigh* Any help, ideas, pointing & laughing, etc. would be appreciated.
edit: fskin UBB code... Wherever it says [ i ] it should have the spaces removed.
My problem is, I've been tasked with reading in a substitution file and doing encryption stuff with it.
The file's format is:
A X
B N
C Y
D A
etc.
where the first letter is the decrypted letter and the second letter is its encrypted counterpart. I'm having problems with getting the file read into an array, which would then be passed to a constructor of a different class to do more stuff with it (per assignment specifications).
Heres what I have so far, using the file C:\substitutiontable.txt:
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
//located in the main method
{
fileLocation=(getInputString("Enter the location of the substitution table: "));
char[][] subTable= fileReaderToArray(fileLocation);
for (int i=1; i < subTable.length; i++)
{
for (int j=0;j < subTable.length; j++)
{
System.out.println(i + " "+ j+" " + subTable[ i ] [j]);
}
}
}
//end main method after doing some more stuff
//helper method for substitution operation, reads subs. table into a 2D array of type char
private static char[][] fileReaderToArray(String fileInput) throws FileNotFoundException,IOException
{
FileReader subTable=new FileReader(fileInput);
char[][] cbuf=new char[3][26];
while (subTable.ready()){
for (int i=1; i < cbuf.length; i++) {
for (int j=0; j < cbuf[ i ] .length;j++){
cbuf [ i ] [j]=(char)subTable.read(); }}}
subTable.close();
return cbuf;
}
</pre><HR></BLOCKQUOTE>
I know I'm missing something, i just dont know what. It doesnt help that I'm a little rusty on my Java skills, having skipped a semester since my last course in it. *sigh* Any help, ideas, pointing & laughing, etc. would be appreciated.
edit: fskin UBB code... Wherever it says [ i ] it should have the spaces removed.