次のtxtファイルからコンマを削除しようとしています:
abcd,efgh,ijkl
mnop,qrst,uvwx
yzzz,0123,4567
8910
私のコードは次のようになります。
public static ArrayList readFileByLine(ArrayList list, String fileName){
try{
File file = new File(fileName);
Scanner reader = new Scanner(file);
reader.useDelimiter(",");
while(reader.hasNext()){
String s = reader.next();
s= s.trim();
s= s.replaceAll("," , "");
list.add(s);
}
reader.close();
}
catch(FileNotFoundException e){ System.err.println("Error: " + e.getMessage());}
return list;
}
絶対に必要でない限り、正規表現を使用しないようにしています。正規表現を使用することをお勧めする場合は、それが何をするのか説明してください! 助けてくれてありがとう!