Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading from Text File and Processing Information

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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
    int (<< data type)
    4 6 (<< row by column)
    (random numbers go here)
    Last edited by royalslim; December 13th, 2010 at 03:09 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading from Text File and Processing Information

    See Java IO Turorial for how to work with files.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from Text File and Processing Information

    Quote Originally Posted by copeg View Post
    See Java IO Turorial for how to work with files.
    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?

Similar Threads

  1. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  2. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM