* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Reading a substitution table from a file
PostPosted: Wed Feb 11, 2004 10:45 pm 
Offline
DBB Benefactor
DBB Benefactor
User avatar

Joined: Mon Dec 16, 2002 3:01 am
Posts: 3166
Website: http://www.mletech.com
Location: southern IL, USA
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.


Top
   
 Post subject:
PostPosted: Fri Feb 13, 2004 11:42 pm 
Offline
DBB Head Flapper
DBB Head Flapper
User avatar

Joined: Thu Nov 05, 1998 12:01 pm
Posts: 2184
Website: http://www.descent2.com/sickone
Location: Mountain View, CA, USA
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?


Top
   
 Post subject:
PostPosted: Sun Feb 15, 2004 5:29 pm 
Offline
DBB Benefactor
DBB Benefactor
User avatar

Joined: Mon Dec 16, 2002 3:01 am
Posts: 3166
Website: http://www.mletech.com
Location: southern IL, USA
[*] your nested loop in the main method is checking the wrong length against j

Oh yeah, didnt see that. Fixed.

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

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?

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>


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 3 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group