Results 1 to 3 of 3
Thread: Arrays and Matrices
 11092012, 10:56 PM #1Member
 Join Date
 Feb 2012
 Location
 Virginia
 Posts
 10
 Rep Power
 0
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?
 11092012, 11:46 PM #2Member
 Join Date
 Mar 2012
 Posts
 88
 Rep Power
 0
Re: Arrays and Matrices
Would you use the scanner class to scan for nextInt and store them for later calculations?
 11102012, 07:24 AM #3Member
 Join Date
 Feb 2012
 Location
 Virginia
 Posts
 10
 Rep Power
 0
Re: Arrays and Matrices
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()){
stringedints.add(in.next());
height++;
}
//finds vertical height
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Similar Threads

how to add columns from Matrices(2D array)
By John_shok in forum New To JavaReplies: 7Last Post: 10042010, 04:33 PM 
Read two matrices from file
By zenitis in forum New To JavaReplies: 13Last Post: 05202010, 03:03 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 01:40 AM 
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:50 PM 
implementing sparse and nonsparse matrices together
By ishakteyran in forum New To JavaReplies: 0Last Post: 12072007, 08:10 PM
Bookmarks