Reading from Text File and Processing Information
Hello I have made a code that can add and subtract two matricies of any size.
The code is:
Code :
import java.util.Scanner;
public class MatrixAlgebra
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Size of matrix A and B
int rowsA;
int colsA;
int rowsB;
int colsB;
//decision values
int operation;
int dataType;
//matrix A and B
int[][] aIntMatrix;
int[][] bIntMatrix;
int[][] cIntMatrix;
double[][] aDblMatrix;
double[][] bDblMatrix;
double[][] cDblMatrix;
//get the size of the matrices
System.out.print("Enter the number of rows and number of columns of matrix A (row column): ");
rowsA = input.nextInt();
colsA = input.nextInt();
System.out.print("Enter the number of rows and number of columns of matrix B (row column): ");
rowsB = input.nextInt();
colsB = input.nextInt();
//get the datatype of the matrix entries
System.out.print("To enter integers press 0, for doubles press 1: ");
dataType = input.nextInt();
//get the operation that will be performed
System.out.print("To add the matrices enter 0, to subtract the matrices enter 1: ");
operation = input.nextInt();
if(dataType == 0)
{
aIntMatrix = new int[rowsA][colsA];
bIntMatrix = new int[rowsB][colsB];
populateMatrix(aIntMatrix, 'A');
populateMatrix(bIntMatrix, 'B');
switch(operation)
{
case 0:
cIntMatrix = addMatrix(aIntMatrix, bIntMatrix);
System.out.println("\nA is: ");
printMatrix(aIntMatrix);
System.out.println("\nB is: ");
printMatrix(bIntMatrix);
System.out.println("\nC is: ");
printMatrix(cIntMatrix);
break;
case 1:
cIntMatrix = subtractMatrix(aIntMatrix,bIntMatrix);
System.out.println("\nA is: ");
printMatrix(aIntMatrix);
System.out.println("\nB is: ");
printMatrix(bIntMatrix);
System.out.println("\nC is: ");
printMatrix(cIntMatrix);
break;
}
}
else if(dataType == 1)
{
aDblMatrix = new double[rowsA][colsA];
bDblMatrix = new double[rowsB][colsB];
populateMatrix(aDblMatrix, 'A');
populateMatrix(bDblMatrix, 'B');
switch(operation)
{
case 0:
cDblMatrix = addMatrix(aDblMatrix, bDblMatrix);
System.out.println("\nA is: ");
printMatrix(aDblMatrix);
System.out.println("\nB is: ");
printMatrix(bDblMatrix);
System.out.println("\nC is: ");
printMatrix(cDblMatrix);
break;
case 1:
cDblMatrix = subtractMatrix(aDblMatrix,bDblMatrix);
System.out.println("\nA is: ");
printMatrix(aDblMatrix);
System.out.println("\nB is: ");
printMatrix(bDblMatrix);
System.out.println("\nC is: ");
printMatrix(cDblMatrix);
break;
}
}
}
// This method is used to populate the two matrices with values (int) entered by the user
public static void populateMatrix(int[][] a, char matrix)
{
Scanner input = new Scanner(System.in);
int row;
int col;
System.out.println("Enter the rows and columns of matrix " + matrix +": ");
for(row = 0; row < a.length; row++)
{
for(col = 0; col < a[row].length; col++)
{
a[row][col] = input.nextInt();
}
}
}
// This method is used to populate the two matrices with values (doubles) entered by the user
public static void populateMatrix(double[][] a, char matrix)
{
Scanner input = new Scanner(System.in);
int row;
int col;
System.out.println("Enter the rows and columns of matrix " + matrix +": ");
for(row = 0; row < a.length; row++)
{
for(col = 0; col < a[row].length; col++)
{
a[row][col] = input.nextDouble();
}
}
}
//This method adds the two matrices (for matrices filled with integers)
public static int[][]addMatrix(int[][]a, int[][]b)
{
int row;
int col;
int[][] c = new int[a.length][a[0].length];
for(row = 0; row < a.length; row++)
for(col = 0; col < a[row].length; col++)
c[row][col] = a[row][col] + b[row][col];
return c;
}
//This method add the two matrices (for matrices filled with doubles)
public static double[][]addMatrix(double[][]a, double[][]b)
{
int row;
int col;
double[][] c = new double[a.length][a[0].length];
for(row = 0; row < a.length; row++)
for(col = 0; col < a[row].length; col++)
c[row][col] = a[row][col] + b[row][col];
return c;
}
//This method subtracts the two matrices (for matrices filled with integers)
public static int[][]subtractMatrix(int[][]a, int[][]b)
{
int row;
int col;
int[][] c = new int[a.length][a[0].length];
for(row = 0; row < a.length; row++)
for(col = 0; col < a[row].length; col++)
c[row][col] = a[row][col] - b[row][col];
return c;
}
//This method subtracts the two matrices (for matrices filled with doubles)
public static double[][]subtractMatrix(double[][]a, double[][]b)
{
int row;
int col;
double[][] c = new double[a.length][a[0].length];
for(row = 0; row < a.length; row++)
for(col = 0; col < a[row].length; col++)
c[row][col] = a[row][col] - b[row][col];
return c;
}
//This method prints the two matrices input by the user and the calculated matrix C
public static void printMatrix(int[][] matrix)
{
int row;
int col;
for(row = 0; row < matrix.length; row++)
{
for(col = 0; col < matrix[row].length; col++)
System.out.printf("%5d", matrix[row][col]);
System.out.println();
}
}
public static void printMatrix(double[][] matrix)
{
int row;
int col;
for(row = 0; row < matrix.length; row++)
{
for(col = 0; col < matrix[row].length; col++)
System.out.printf("%5.2f", matrix[row][col]);
System.out.println();
}
}
}
Now instead of asking for user input, the input needs to come from a txt file and an output file needs to be written. How would I approach editing the code to extract the information?
The input.txt will look like
Quote:
int (<< data type)
4 6 (<< row by column)
(random numbers go here)
Re: Reading from Text File and Processing Information
See Java IO Turorial for how to work with files.
Re: Reading from Text File and Processing Information
Quote:
Originally Posted by
copeg
Theres going to be an InputA.txt (First Matrix) and InputB.txt (Second Matrix). The user will need to specify the name of these two files. So how would you recommend I use Scanner to replace where I got input the first way?