Lab 11 Download Code For Students

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

//Student make additions to code

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class LabIO


{
private double [][] data; // rows = data.length;
// cols = data[i].length;
public static void main( String [] args )
// throws IOException
{
LabIO fileTest = new LabIO();
fileTest.readData();
System.out.println( fileTest.toString() );
fileTest.writeData();

}
public void readData() // throws IOException
//public void readData() // throws IOException
{
Scanner console = new Scanner( System.in );
String outMsg = "Enter the input data filename (include .txt)";
// use Lab11.txt
Scanner tokens = null;
boolean repeat;
console.close();

do {

repeat = false;
String inFile = JOptionPane.showInputDialog( outMsg );

try {
File file = new File( inFile );
tokens = new Scanner( file );

// STUDENT CODE: get the number of rows


int nrows =

// STUDENT CODE: get the number of rows


int ncols =
//STUDENT CODE: allocate space for the 2-d data array instance field:
data =

//STUDENT CODE:
//write a nested loop to read the data into the data array.

//close the file:


} catch ( FileNotFoundException err ) {
System.out.println( err );
repeat = true;
} finally {
if ( tokens != null ) tokens.close();
}

} while ( repeat );

} // end readData()

public void writeData() // throws IOException


{
//prompt using the java.swing package for a file name to write.

String outMsg = "Enter the output filename: ";

String outFile = JOptionPane.showInputDialog( outMsg );


//Allocate the file and allow buffering with printf
PrintWriter out = null;
try {
FileWriter writeIt = new FileWriter( outFile );
out = new PrintWriter( writeIt );

//write the number of rows into the file


out.printf( "%d\n", data.length );
//write the number of columns into the file
out.printf( "%d\n", data[0].length ); // all rows same length

//Use the toString method to create a string and write into your output file.
String myData = this.toString();
out.printf( "%s", myData );

//STUDENT CODE: Write your name into the file:


out.printf("%s\n", "YOUR NAME IN HERE" );
} catch ( IOException err ) {
System.out.println( err );
}
//close the file.
if ( out != null ) out.close();

} // end of writeData

public String toString( )


{
//STUDENT CODE :
}

} // end class

You might also like