created 06/16/00

Programming Exercises


Exercise 1 --- Data Translator

Write a program that asks the user for the name of an input text file. The text file (which could be created with NotePad) will contain integers in character format, one per line, such as follows.

12
1023
-56
84781
0
-9371

The program also asks for the name of an output file. The program then reads the input file line by line until end of file. Each input integer is translated into int data (use Integer.parseInt()) and written to the output file using DataOutputStream.writeInt().

Look at the input file and output file with the DOS command DIR and, if possible, with a hex dump program.


C:\Programs>java  IntCopy
Input file -->ints.txt
Output file -->ints.bin

C:\Programs>dir

06/16/00  04:33p                 1,881 IntCopy.class
06/16/00  04:33p                 1,920 IntCopy.java
06/16/00  04:35p                    24 ints.bin
06/16/00  04:35p                    30 ints.txt

Write the input loop so it catches NumberFormatExceptions and echos the offending line. Here is another input file:

0
1
2
3
4
Rats, this won't work
6
7
8

And the corresponding run of the program:

C:\Programs>dir
Input file -->oops.txt
Output file -->opps.bin
Poor input data:Rats, this won't work

C:\Programs>
Click here to go back to the main menu.

End of Exercises.