从文本文件java scanner中读取2d数组

时间:2012-08-30 03:59:38

标签: java arrays 2d

我一直在:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Driver0.readFile(Driver0.java:38)
at Driver0.main(Driver0.java:18)

尝试使用扫描仪类,因为到目前为止我都知道。任何帮助赞赏。试图读取2d数组但它从未完全读取它。我的input.txt文件是:

3 5

2 3 4 5 10

4 5 2 3 7

-3 -1 0 1 5

import java.io.*;
import java.util.*;
public class Driver0 {
//public static String fileName; // declare so it may be used outside of main
public static int[][] array; // declare as empty until given dimensions
public static int dimensionA, dimensionB; // dimensions used in methods
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the name of the data file?");
    System.out.print("> ");
    String fileName = input.nextLine();
    readFile(fileName);
    String nextCommand = "";
    while (!(nextCommand.equals("quit"))) {
        System.out.println("\nNext command");
        System.out.print("> ");
        nextCommand = input.nextLine();
        choice (nextCommand);
    }
}
public static void choice(String command) {
    switch (command) {
        case "help": System.out.println("show array\nrows sorted\ncols sorted"+
                    "increase row i by n\nincrease col j by n\nquit\n");
                break;
        default: showArray();
    }
}
public static void readFile(String fileName) {
    try {
        Scanner foo = new Scanner(fileName);
        dimensionA = foo.nextInt();
        dimensionB = foo.nextInt();

        array = new int[dimensionA][dimensionB];
        while (foo.hasNext()) {
            for (int row = 0; row < dimensionA; row++) {
                foo.nextLine();
                for (int column = 0; column < dimensionB; column++) {
                    array[row][column] = foo.nextInt();
                }
            }
        }
        foo.close();
    }
    catch(Exception e) {
        e.printStackTrace(); // displays type of error
    }
}
public static void showArray() {
    for (int ro = 0; ro < dimensionA; ro++) {
        for (int col = 0; col < dimensionB; col++) {
            System.out.print(array[ro][col]);
        }
        System.out.println();
    }
}
}

2 个答案:

答案 0 :(得分:0)

浏览Scanner课程中的方法。

特别是hasXXX()nextXXX()方法,它们成对出现。尽量不要混合匹配那些会使事情变得复杂的方法。您使用的是nextInt()hasNext()nextLine()。文档还解释了这些方法在调用它们时的作用。

对于您的情况,如果您愿意使用String.split(),方法hasNextLine()nextLine()就足够了。您可以拆分nextLine()返回的字符串并解析整数并将其分配给数组元素。

答案 1 :(得分:0)

我认为最好使用ArrayList。试试这个

package readfile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Read {

public static void main(String[] args) throws FileNotFoundException, IOException {

    String line = null;
    List<String[]> list = new ArrayList<String[]>();
    BufferedReader reader = new BufferedReader(new FileReader("/home/user/My_Folder/read_me.txt"));
    while ((line = reader.readLine())!=null) { 
        list.add(getArray(line));
    }        
    reader.close();

    for (String[] stringArr : list) {
        for(String str : stringArr){
            System.out.print(str+" ");
        }
        System.out.println("");
    }

}

private static String[] getArray(String s){
    String[] array = s.split("\\s");
    return array;
}

}
相关问题