0

ファイルを配列に読み込みましたが、その配列から特定の値を解析する方法を知りたいです。

私のコード:

        ...
        try{
             ...
             String strLine;
             String delims= "[ ]+";
             //parsing it
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             System.out.println("Length:" + parsedit.size());
             in.close();   
             ...

私が読んでいるファイルは次のようなものです:

a b c d e f g
1 2 3 4 5 6 7
1 4 5 6 3 5 7
1 4 6 7 3 2 5

出力は次のようになります。

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...
Length:4

このデータを解析して、1 番目と 5 番目の値だけを残して、代わりに次のように読みたいと思います。

a e
1 5

誰かがそれを行う方法についての推奨事項を持っていますか? 編集:いくつかの提案に従って、コードを次のように変更しました。

public static void main(String[] args) {
        System.out.print("How many files will be input? ");
        Scanner readIn=new Scanner(System.in);
        int input=readIn.nextInt();
        int i=1;
        while(i<=input){
            System.out.println("Hey, please write the full path of the input file number" + i + "! ");
            Scanner fIn=new Scanner(System.in);
            String fileinput=fIn.nextLine();
          try{
             FileInputStream fstream=new FileInputStream(fileinput);
             DataInputStream in=new DataInputStream(fstream);
             BufferedReader br=new BufferedReader(new InputStreamReader(in));
             String strLine;
             String delims= "[ ]+";
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             String[] splits=strLine.split(delims);
             System.out.println("Length:" + splits.length);
             in.close();   
        }
        catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
        i++;
    }
  }
}

これによりエラーは返されませんが、まったく同じようには機能していないようです。私は何かばかげたことを見逃していますか?出力は次のとおりです。

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...

しかし、配列の長さを示す行があるという事実にもかかわらず、それは印刷されません。これは、修正した以上に壊れてしまったことを示しています。私が行方不明/忘れている可能性があるのは何か知っていますか?

4

3 に答える 3

1

The split() function returns an array of strings representing the tokens obtained from the original String.

So, what you want is to keep only the first and the 5th token (splits[0] & splits[4]):

  String[] splits = strLine.split(delims); 
  //use the splits[0] & splits[4] to create the Strings you want

Regarding your update, replace this:

 while((strLine=br.readLine())!=null){
             System.out.println("Being read:");
             System.out.println(strLine);
             parsedit.add(strLine.split(delims));
 }

With this:

 while((strLine=br.readLine())!=null){
             System.out.println("Being read:");
             String splits[] = strLine.split(delims);
             System.out.println(splits[0]+" "+splits[4]);
             parsedit.add(splits);
 }
于 2012-08-23T14:51:47.643 に答える
0

You can try this

String[] line = s.split("\\s+");

This way all your elements will be stored in a sequence in the array line. This will allow you to access whatever element you want.

于 2012-08-23T14:51:37.610 に答える
0

String[]によって返された から最初と 5 番目の値を取得するだけですstrLine.split(delims)

(現在のコードで何をしているのかを知っていると仮定しています。各行には、少なくとも1つのスペース文字で区切られた同じ数の「列」が含まれていると仮定しています。それは、それにもかかわらず、公正な仮定です。)

于 2012-08-23T14:52:36.560 に答える