Question
Write a program described on the next page of this document. You must use a Java IDE to complete this assignment. Use IOLab.java to do
Write a program described on the next page of this document. You must use a Java IDE to complete this assignment. Use IOLab.java to do the following:
1. Your finished program must exclude all the throws IOException lines. Remove the throws IOException from the readData first. Don't remove the throws IOExcpetion from the main method until the program is completed and everything else working.
2. Include competent exception handlers for the readData and writeData methods in this assignment.
3. See the example IO programs posted on the course web-site for assistance.
4. The file to read is located on the last page of this document. Copy and paste the data on the last page into a simple text file.
5. The first two entries in the file represent: 1) the number of rows, 2) and the number of columns. Read the remaining numbers from the input file data and fill a two-dimensional array by rows. For example, if the number of rows is 6 and the number of columns is 4, then the next 4 numbers in the input file make up the first row of numbers in your 2-D array. Use a nested loop to fill the 2-D array.
6. Write an instance method and name it toString. This method will return a string containing the 2-D array as a table. Use the String.format( ) method to build your string. The decimal point should be aligned in each column of output.
7. Prompt the user for an output file-name to write. Write the number of rows and columns into the first two lines of the file. The output file name must be different than the input file name.
8. Write the contents of your 2-D data array into the file using the printf method to format your values in a row-column format using format elements. The decimal point should be aligned in each column. Write the data with four digits to the right of the decimal.
9. Add logic to recover from an exception in the readData method. If the user enters a nonexistent file name, prompt the user for another file name.
public class IOLab // rows // data. Iength; data [i].length; private double [][] data; cols public static void main( String [ args throws IOException 10Lab fileTest new 10Lab ( ); fileTest.readData System.out.println( fileTest.tostring) fileTest.writeData) public void readData() throws IOException Scanner console new Scanner ( System.in ); System.out.print "Please enter a file name to read: " String inFile = console . next(); /I connect to the file File file = new File ( inFile ); scanner tokens = new scanner( file ); // buffer the data int nrows = tokens.nextInt(); // get the number of rows. // 1. get the number of cols // 2. allocate space for the 2-d data array instance field: 11 3. write a nested loop to read the data into the data array /1 4. close the file: ) // end readData () public void writeData throws IOException
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started