私はすべての非数字文字で与えられた入力を分割するこの簡単なプログラムを書いています。文字列の間違った出力を分割する
public class Fileread {
public static void main(String[] args) throws IOException {
//Declarations
String[] temp;
String current;
//Execution
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
while ((current = br.readLine()) != null) {
temp = current.split("\\D"); //Splitting at Non Digits
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
}
}
これはINPUT.TXTです:
hello1world2
world3
end4of5world6
は出力:
1
2
3
4
5
6
なぜこれほど多くの余分なスペースが表示されるのですか?私は間にスペースを入れずに、それぞれの番号を別々の行に印刷する必要があります。これをどうすれば解決できますか?
使用 '\\ D +'パターンを参照してください。しかし、文字列が非数字で始まる場合、先頭の空の要素は保持されます。 –