0

次のようなエントリを持つログ ファイルを調べるプログラムを作成しています。

en halo%20reach%20noble%20actual%20in%20theater 1 659
en Wazir_Khan_Mosque 2 77859
en Waziristan_War 3 285976
en Wazirpur_Upazila 1 364

各文字列の末尾に表示される数字 (つまり、659、77859、285976、285976、364) を出力したいと考えています。ご覧のとおり、数字の桁数はさまざまです。

これらの文字列から最後の数字を取得するにはどうすればよいですか?

4

5 に答える 5

0

各行を読んで、このような文字列に割り当てる場合

String line = "en halo%20reach%20noble%20actual%20in%20theater 1 659";

これを行うと、最後の番号が得られます

String words[] = line.split("\\s");
System.out.println(words[words.length - 1]);
于 2013-09-08T15:55:02.117 に答える
0

これを試してください、あまり良くないかもしれません

public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader br = new BufferedReader(new FileReader("log.txt"));
try {
    String line = br.readLine();
    List<String> stringList = new ArrayList<>();
    while(line!=null) {
        String[] strsplit = line.split(" ");
        line = br.readLine();
        for(int i=3;i<strsplit.length;i+=4) {
            stringList.add(strsplit[i]);
        }
    }
    System.out.println(stringList);
} finally {
    br.close();
}
于 2013-09-08T16:19:16.230 に答える