ファイルを配列に読み込みましたが、その配列から特定の値を解析する方法を知りたいです。
私のコード:
...
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
...
しかし、配列の長さを示す行があるという事実にもかかわらず、それは印刷されません。これは、修正した以上に壊れてしまったことを示しています。私が行方不明/忘れている可能性があるのは何か知っていますか?