Reading a substitution table from a file

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Vindicator
DBB Benefactor
DBB Benefactor
Posts: 3166
Joined: Mon Dec 16, 2002 3:01 am
Location: southern IL, USA
Contact:

Reading a substitution table from a file

Post by Vindicator »

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.
User avatar
MehYam
DBB Head Flapper
DBB Head Flapper
Posts: 2184
Joined: Thu Nov 05, 1998 12:01 pm
Location: Mountain View, CA, USA
Contact:

Post by MehYam »

I barely remember how the file I/O works, I just vaguely recall it working differently than you've listed in your example. I have three half-hearted suggestions followed by a fourth full-hearted one:

[*] your nested loop in the main method is checking the wrong length against j
[*] is subTable.ready() the right thing to call?
[*] why are you casting subTable.read()'s result to a char? What is it supposed to return?
[*] put a System.error.println(...) in strategic places in your code to help you debug. This is a basic skill that you'll need to get through this course's assignments. Also, you didn't mention anything about how this is failing. Is it throwing an exception? Is it outputting anything at all?
User avatar
Vindicator
DBB Benefactor
DBB Benefactor
Posts: 3166
Joined: Mon Dec 16, 2002 3:01 am
Location: southern IL, USA
Contact:

Post by Vindicator »

[*] your nested loop in the main method is checking the wrong length against j[/b]

Oh yeah, didnt see that. Fixed.

[*] is subTable.ready() the right thing to call?[/b]

According to the Java API, the FileReader's ready method "tells whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream." I figured it was better than using <tt>while (subTable.read() != -1)</tt>.

[*] why are you casting subTable.read()'s result to a char? What is it supposed to return?[/b]

If I dont cast it as a char the compiler bitches about a possible loss of precision since apparently <tt>subTable.read()</tt> returns integers. Like I said, its ****ed up.[*] put a System.error.println(...) in strategic places in your code to help you debug. This is a basic skill that you'll need to get through this course's assignments. Also, you didn't mention anything about how this is failing. Is it throwing an exception? Is it outputting anything at all?

Here's the complete substitution table that I'm using:
<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>A X
B N
C Y
D A
E H
F P
G O
H G
I Z
J Q
K W
L B
M T
N S
O F
P L
Q R
R C
S V
T M
U U
V E
W K
X J
Y D
Z I</pre><HR></BLOCKQUOTE>


And here's the array, using <tt>System.out.println(i+" "+j+" "+subTable[ i ][j])</tt> in the main method :

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>1 0 E
1 1
1 2
1 3 W
1 4
1 5 K
1 6
1 7
1 8 X
1 9
1 10 J
1 11
1 12
1 13 Y
1 14
1 15 D
1 16
1 17
1 18 Z
1 19
1 20 I
1 21 ?
1 22 ?
1 23 ?
1 24 ?
1 25 ?
2 0 ?
2 1 ?
2 2 ?
2 3 ?
2 4 ?
2 5 ?
2 6 ?
2 7 ?
2 8 ?
2 9 ?
2 10 ?
2 11 ?
2 12 ?
2 13 ?
2 14 ?
2 15 ?
2 16 ?
2 17 ?
2 18 ?
2 19 ?
2 20 ?
2 21 ?
2 22 ?
2 23 ?
2 24 ?
2 25 ?</pre><HR></BLOCKQUOTE>
Post Reply