
Patrick B. answered 05/02/20
Math and computer tutor/teacher
import java.io.*;
class AngieARaggedInput
{
public static final int MAX=10;
double [][] readFile( File file)
{
String inbuff;
double [][] A = new double[MAX][MAX];
//initializes the array
for (int i=0; i<MAX; i++)
{
for (int j=0; j<MAX; j++)
{
A[i][j]=0;
}
}
try
{
String fileName = file.getName();
BufferedReader bufferedReader = new BufferedReader( new FileReader(fileName));
for (int iLoop=0; iLoop<MAX; iLoop++)
{
inbuff = bufferedReader.readLine();
if (inbuff==null) { break; }
String tokens[] = inbuff.split(" ");
int iIndexPos = 0;
for (int jLoop=0; jLoop<tokens.length; jLoop++) // for each token that is parsed....
{
if (iIndexPos<MAX) // the array is not full
{
if (tokens[jLoop].length()>0) // the token has data
{
A[iLoop][iIndexPos++] = Double.parseDouble(tokens[jLoop]); //converts to double and stores it
}
}
else // the array is full...bails out
{
break;
}
}
} //for iLoop
} //try
catch (IOException ex)
{
}
return(A);
} //readFile
public static void main(String args[])
{
File infile = new File ("dblAmounts.dat");
AngieARaggedInput x = new AngieARaggedInput();
double A[][] = x.readFile(infile);
for (int iLoop=0; iLoop<A.length; iLoop++)
{
for (int jLoop=0; jLoop<A[iLoop].length; jLoop++)
{
if (A[iLoop][jLoop]!=0)
{
System.out.print(A[iLoop][jLoop] + " ");
}
}
System.out.println(" ");
}
}
}