-2

インポートしたファイルを配列リストに入れ、それを分解して次のようにする必要があるプログラムに取り組んでいます。

00000000    100 80  100 20  20  40  50
12345687    98  67  88  20  19  37  39
89763465    67  79  90  18  16  35  44

最初の数字を 1 つのリストなどとして読み取りたい。

public void studentData() throws IOException {
    Scanner scanner = new Scanner(System.in);

    ArrayList<String> list = new ArrayList<String>();
    System.out.println("Enter a file name: ");
    String filename = scanner.nextLine().trim();

    filename.equalsIgnoreCase(filename);
    File f = new File(filename);
    Scanner file = new Scanner(f);
    String fileText = file.nextLine();

    while (file.hasNext()){
        list.add(file.next());
    }
    file.close();

まったく何も印刷されていません。肯定的なフィードバックをいただければ幸いです。ありがとう!

4

2 に答える 2

0

あなたのコードは機能しています、

しばらく :

ファイルがで始まらない場合\n は削除します

 String fileText = file.nextLine();

これを追加します:

for (String string : list) {
            System.out.println(string);
        }

最後に、値を出力します。

于 2013-09-20T20:40:44.130 に答える
0

最初の数字を 1 つのリストなどとして読み取りたい。

次のように、定義したリストに数値のすべての行を格納できます。

    Scanner scanner = new Scanner(System.in);

    ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
    System.out.println("Enter a file name: ");
    String filename = scanner.nextLine().trim();

    filename.equalsIgnoreCase(filename); // dose't do anything?!!!!
    File f = new File(filename);
    Scanner file = new Scanner(f);
    //String fileText = file.nextLine(); this line removed the first line of txt

    while (file.hasNext()) {
        list.add(new ArrayList(Arrays.asList(file.nextLine().replaceAll("\\D+",",").split(",")))); // this line is what you need
    }
    file.close();

それがあなたのニーズであることを願っています

于 2013-09-20T21:22:09.913 に答える