# Arrays and Matrices

• 11-09-2012, 11:56 PM
Shaeman111
Arrays and Matrices
I have to make a method...

public static void multiply(File file)

This method receives a file object from which to read 2 numeric matrices. The method multiplies the matrices and writes the result matrix back to the given file.
Matrices in the input file are placed side by side. For example, the data below represents a 4x2 matrix (left) and a 2x3 matrix (right). Note that row 1 of both matrices is always on the first line of the file, and that there will be no blank lines in the file.
1 2 1 2 3
3 4 4 5 6
5 6
7 8

So my question is... How would I go about separating the matrices? Or even more specifically, differentiating the two in java?
The professor never taught us this in class so I assume he expects us to get creative.. But I've been stuck for a week. Any ideas?
• 11-10-2012, 12:46 AM
lannie1980
Re: Arrays and Matrices
Would you use the scanner class to scan for nextInt and store them for later calculations?
• 11-10-2012, 08:24 AM
Shaeman111
Re: Arrays and Matrices
Quote:

Originally Posted by lannie1980
Would you use the scanner class to scan for nextInt and store them for later calculations?

Yeah, absolutely.
I tried thinking about "if I can set the first row as an array of integers, and the second row as an array, and the third... etc, and then figure out when the size of an array becomes different, i'll know where to separate them."
so like.. in the case of a the matrices
Array0- 6 7 1 0
Array1- 0 2 5 7
Array2- 1 3
Array3- 9 4

when the scanner reads that array2's length is less, then we will know to separate them ... but how do I go about separating the two?

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class Matrix {
public static void multiply(File file){
// ArrayList<ArrayList<Integer>> originalGrid = new ArrayList<ArrayList<Integer>>();
boolean endoflines = false;
ArrayList<String> stringedints = new ArrayList<String>();

try {
int height = 0;
Scanner in = new Scanner( file );
while(!endoflines){
while(in.hasNext()){