Results 1 to 3 of 3
  1. #1
    Shaeman111 is offline Member
    Join Date
    Feb 2012
    Location
    Virginia
    Posts
    10
    Rep Power
    0

    Default 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?

  2. #2
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Arrays and Matrices

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

  3. #3
    Shaeman111 is offline Member
    Join Date
    Feb 2012
    Location
    Virginia
    Posts
    10
    Rep Power
    0

    Default Re: Arrays and Matrices

    Quote Originally Posted by lannie1980 View Post
    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()){
    stringedints.add(in.next());
    height++;
    }
    //finds vertical height
    }
    in.close();


    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }

Similar Threads

  1. how to add columns from Matrices(2D array)
    By John_shok in forum New To Java
    Replies: 7
    Last Post: 10-04-2010, 04:33 PM
  2. Read two matrices from file
    By zenitis in forum New To Java
    Replies: 13
    Last Post: 05-20-2010, 03:03 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM
  5. implementing sparse and nonsparse matrices together
    By ishakteyran in forum New To Java
    Replies: 0
    Last Post: 12-07-2007, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •