Ask Experts Questions for FREE Help !
Ask
    citydude23's Avatar
    citydude23 Posts: 1, Reputation: 1
    New Member
     
    #1

    Feb 5, 2016, 03:33 PM
    Array help and file io
    I need help multiplying 2d arrays from and input file (input.txt) and I need help writing their multiplied outputs to and output file(output.txt) Why isn't my method showNewArray working?


    • input.txt:

    • 9 3
    • 8 5

    • 2 3 3 8
    • 2 1 1 2
    • 3 3 5 9

    • 2 2 3
    • 3 4 6
    • 8 9 7 6
    • (edit: sorry wrote the question really fast!)



    • import java.io.BufferedReader;
    • import java.io.FileNotFoundException;
    • import java.io.FileReader;
    • import java.io.FileWriter;
    • import java.io.IOException;

    • public class hw1 {

    • public int[][] showNewArray(int[][] matrix1, int[][] matrix2){// mulitplies two matrixes together
    • int[][] matrix3 = new int[matrix1.length][matrix2.length];// takes the lengths from matrix1&&matrix2
    • for(int i =0;i <matrix1.length;i++){
    • for(int j =0; j<matrix2[0].length;j++){//column length of matrix2
    • for(int k =0; k< matrix1[0].length;k++){//column length of matrix1
    • matrix3[i][j] += matrix1[i][k] * matrix2[k][j];// multiplies em
    • }
    • }
    • }
    • showNewArray(matrix3[][]);
    • }

    • public static void main(String args[]){
    • FileReader fileReader = null;
    • FileWriter fileWriter = null;
    • BufferedReader bufferedReader = null;
    • try {
    • fileReader = new FileReader("C:/javastuff/principles/input.txt");
    • fileWriter = new FileWriter("C:/javastuff/principles/output.txt",true);
    • bufferedReader = new BufferedReader(fileReader);

    • int flag=0;
    • String line = null;//initialization
    • String words[];
    • int lineCount = 0;
    • int row1=0, column1=0, row2=0, column2=0;
    • String fileData[] = new String[100];//we are assuming the file won't contain more than 100 lines, but this is clearly a drawback
    • while ((line = bufferedReader.readLine()) != null){

    • System.out.println(line);//to check that code is reading file line by line
    • if(!line.trim().equals("")){//we ignore the blank lines
    • words = line.trim().split("\\s+"); //split the line in words separated by spaces
    • if(lineCount==0){
    • row1 = Integer.parseInt(words[0]);
    • column1 = Integer.parseInt(words[1]);
    • }
    • else if(lineCount==1){
    • row2 = Integer.parseInt(words[0]);
    • column2 = Integer.parseInt(words[1]);

    • if(column1!=row2){
    • flag = 1;
    • break;
    • }

    • }
    • else{
    • fileData[lineCount] = line;
    • }
    • lineCount++;
    • }
    • }
    • if(flag==1){
    • System.out.println("Invalid input.");
    • fileWriter.write("Invalid input.");

    • }else{
    • int[][] matrix1=new int[row1][column1];
    • int[][] matrix2=new int[row2][column2];

    • int fileRow=0;
    • for(int index=2;index<row1+2;index++) {
    • line = fileData[index];

    • if(!line.trim().equals("")){//we ignore the blank lines
    • words=line.trim().split("\\s+");
    • for (int col = 0; col < words.length; col++) {
    • matrix1[fileRow][col] = Integer.parseInt(words[col]);
    • }
    • fileRow++;
    • }
    • }
    • System.out.println("matrix1:");
    • fileWriter.write("\nmatrix1:\n");
    • for(int p =0;p<row1;p++){
    • for(int q =0;q<column1;q++){
    • System.out.print(matrix1[p][q]+" ");
    • fileWriter.write(matrix1[p][q]+" ");
    • }
    • System.out.println();
    • fileWriter.write("\n");
    • }
    • fileRow=0;
    • for(int index=row1+2;index<row2+row1+2;index++) {
    • line = fileData[index];
    • if(!line.trim().equals("")){//we ignore the blank lines
    • words=line.trim().split("\\s+");
    • for (int col2 = 0; col2 < words.length; col2++) {
    • matrix2[fileRow][col2] = Integer.parseInt(words[col2]);
    • }
    • fileRow++;
    • }
    • }
    • System.out.println("matrix2:");
    • fileWriter.write("\nmatrix2:\n");

    • for(int p =0;p<row2;p++){
    • for(int q =0;q<column2;q++){
    • System.out.print(matrix2[p][q]+" ");
    • fileWriter.write(matrix2[p][q]+" ");
    • }
    • System.out.println();
    • fileWriter.write("\n");
    • }
    • }

    • bufferedReader.close();
    • fileReader.close();
    • fileWriter.close();

    • }
    • catch (NumberFormatException e) {
    • e.printStackTrace();
    • } catch (FileNotFoundException e) {
    • e.printStackTrace();
    • } catch (IOException e) {
    • e.printStackTrace();
    • } catch (Exception e) {
    • e.printStackTrace();
    • }
    • showNewArray MpliedTogether = new showNewArray(matrix1[][], matrix2[][]);
    • }
    • }
    Kristofer's Avatar
    Kristofer Posts: 18, Reputation: 1
    -
     
    #2

    Mar 11, 2016, 05:16 PM
    This makes no sense. Is this written by a computer?
    Alty's Avatar
    Alty Posts: 28,317, Reputation: 5972
    Pets Expert
     
    #3

    Mar 11, 2016, 05:35 PM
    Is this homework?

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java: Array Not Working [ 0 Answers ]

Hey everyone, I have an issue where I have a method and I am trying to check if the name of an object is the same as a String parameter for the method. It does not seem to be working for me. If anyone could assist me, I would be grateful: public String toString(String checkName) { int...

Array formula [ 12 Answers ]

I have a lookup table and I need a formula that looks at (example) h14 then i14 then looks up the table and returns the value associated with that row. Here is part of my formula, but its lacking something. =IF(h14<0.01," ",LOOKUP(h14,$A$368:$A$711,$G$368:$G$711)) Can anyone help, please....

Array controller [ 1 Answers ]

Have 4 drives in array. Three show as 1 logical; one shows as unallocated. How do I extend the unallocated to the logical.

An array [ 1 Answers ]

How many ratings do you wish to enter? 10 Rating #1: 9 Rating #2: 5 Rating #3: 9 Rating #4: 2 Rating #5: 11 Invalid entry, score must be in range of 1 and 10; try again. Rating #5: -1 Invalid entry, score must be in range of 1 and 10; try again. Rating #5: 8


View more questions Search