2017-01-23 75 views
1

基本上,我試圖從看起來像這樣的文本文件創建兩個不同大小的二維數組:可能從文件創建兩個不同大小的數組/矩陣?

2 
add 
3 4 
2 1 7 -10 
0 5 -3 12 
1 7 -2 -5 
0 1 2 3 
4 5 6 7 
8 9 0 1 
subtract 
2 2 
2 12 
10 0 
4 6 
9 1 

2是(增加和減少)的問題數,34是行數和列數,以及它下面的數字是兩個單獨的矩陣被填充到二維數組中。如果我只是停在那裏,這個程序正常工作:

class Matrices { 
private Scanner fileReader; 
private int rows; 
private int columns; 
int problems; 
String method; 

public Matrices(String file) throws FileNotFoundException { 
    this.fileReader = new Scanner(new FileInputStream(file)); 
    problems = fileReader.nextInt(); 
    method = fileReader.next(); 

    if(method.equals("add")) { 
     rows = fileReader.nextInt(); 
     columns = fileReader.nextInt(); 
    } 

} 

public int[][] readMatrix() throws FileNotFoundException { 
    int[][] result = new int[rows][columns]; 
    for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < columns; j++) { 
      result[i][j] = fileReader.nextInt(); 
     } 
    } 
    return result; 
} 


public int[][] add(int[][] a, int[][] b) { 
    int[][] result = new int[rows][columns]; 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       result[i][j] = a[i][j] + b[i][j]; 
      } 
     } 
     return result; 
} 

public void printMatrix(int[][] matrix) { 
    for (int[] anArray : matrix) { 
     for (int anInt : anArray) { 
      System.out.print(anInt+ " "); 
     } 
     System.out.println(); 
    } 
} 
} 

有了這個驅動程序:

public class MatricesDriver { 

    public static void main(String[] args) throws FileNotFoundException { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter name of file: "); 
    String filename = keyboard.next(); 

    Matrices matrixReader = new Matrices(filename); 
    int[][] a = matrixReader.readMatrix(); 
    int[][] b = matrixReader.readMatrix(); 

    System.out.println("Matrix 1: "); 
    matrixReader.printMatrix(a); 
    System.out.println(); 
    System.out.println("Matrix 2: "); 
    matrixReader.printMatrix(b); 
    System.out.println(); 
    System.out.println("Addition: "); 
    int[][] addition = matrixReader.add(a,b); 
    matrixReader.printMatrix(addition); 

    } 
} 

它創建和打印矩陣得很好,沒有任何問題。然而,每當我試圖創建和打印接下來的兩個矩陣(下面的文本文件減去2×2陣列),它返回以下錯誤:

Enter name of file: 
data/Matrices.txt 
Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:862) 
    at java.util.Scanner.next(Scanner.java:1485) 
    at java.util.Scanner.nextInt(Scanner.java:2117) 
    at java.util.Scanner.nextInt(Scanner.java:2076) 
    at baker.Matrices.readMatrix(Matrices.java:27) 
    at baker.MatricesDriver.main(MatricesDriver.java:15 

我的問題是,我應該做出什麼樣的調整,以使程序認識到2D陣列中的兩個是3x4,以下兩個是2x2?

+0

是給出的文本格式? – swinkler

+0

該文本文件?是的,就像它在那裏列出的一樣,沒有空格,也沒有單獨的文件 – bgb102

+0

沒有空格我的意思是沒有空格之間的空格,對不明確的道歉 – bgb102

回答

1

我建議分解你的實現到做出的ArrayList以下部分:

  • Matrix - 保存問題定義中的一個矩陣的值
  • Problem - 持有的操作,並從問題定義兩個矩陣

Matrix類可能看起來像:

class Matrix { 
    private int[][] values; 

    public Matrix(int[][] values) { 
     this.values = values; 
    } 

    public int[][] getValues() { 
     return values; 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     sb.append("Matrix [values=\n"); 
     for (int i = 0; i < values.length; i++) { 
      sb.append("\t" + Arrays.toString(values[i]) + "\n"); 
     } 
     sb.append("]"); 
     return sb.toString(); 
    } 
} 

Problem類可能是:

class Problem { 
    private String operation; 
    private Matrix first; 
    private Matrix second; 

    public Problem(String operation, Matrix firstMatrix, Matrix secondMatrix) { 
     this.operation = operation; 
     first = firstMatrix; 
     second = secondMatrix; 
    } 

    public String getOperation() { 
     return operation; 
    } 

    public Matrix getFirst() { 
     return first; 
    } 

    public Matrix getSecond() { 
     return second; 
    } 

    @Override 
    public String toString() { 
     return "Problem [\noperation=" + operation + ", \nfirst=" + first + ", \nsecond=" + second + "\n]"; 
    } 
} 

基礎的在此,您的「駕駛員」級別執行以下操作:

  1. 獲得從用戶輸入的文件名
  2. 從文件中讀取的問題的數量和構造與該初始規模
  3. 列表有關的問題的數量(即2)獲取操作,行和列,並構造一個包含這些信息的新Problem對象,並將該問題放入列表中...

這裏有一個簡單的解決方案 - 仍然在這裏的改進空間:-)

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Scanner; 

public class MatricesDriver { 
    public static void main(String[] args) throws FileNotFoundException { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter name of file: "); 
     String filename = keyboard.next(); 

     List<Problem> problems = readProblems(filename); 
     System.out.println(problems); 
     keyboard.close(); 
    } 

    private static List<Problem> readProblems(final String filename) throws FileNotFoundException { 
     Scanner fileReader = new Scanner(new FileInputStream(filename)); 
     int numberOfProblems = fileReader.nextInt(); 
     List<Problem> problems = new ArrayList<>(numberOfProblems); 
     for (int i = 1; i <= numberOfProblems; i++) { 
      problems.add(readProblem(fileReader)); 
     } 
     fileReader.close(); 
     return problems; 
    } 

    private static Problem readProblem(Scanner fileReader) throws FileNotFoundException { 
     fileReader.nextLine(); // go to next line 
     String operation = fileReader.nextLine(); // read problem operation 
     int rows = fileReader.nextInt(); // read number of rows 
     int columns = fileReader.nextInt(); // read number of columns 
     Matrix firstMatrix = readMatrix(rows, columns, fileReader); 
     Matrix secondMatrix = readMatrix(rows, columns, fileReader); 
     return new Problem(operation, firstMatrix, secondMatrix); 
    } 

    private static Matrix readMatrix(final int rows, final int columns, final Scanner fileReader) throws FileNotFoundException { 
     int[][] result = new int[rows][columns]; 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       result[i][j] = fileReader.nextInt(); 
      } 
     } 
     return new Matrix(result); 
    } 

} 

與輸入文件進行測試。

HTH

+0

代碼的結束遊戲是兩個添加3x4矩陣,並減去2x2矩陣,然後打印它們,我認爲我在正確的軌道上,我更新了我的代碼。 – bgb102

+0

好。您能否將標記爲有幫助或正確的答案來關閉此主題。謝謝。 – swinkler

+0

噢,對不起。你走了。再次感謝 – bgb102

0

您試圖構建您的數據?

您可以使用:

properties-file

對於創建一個沒有固定大小的使用ArrayList數組或向量,你可以從一個ArrayList中

相關問題