0

からテキストをBufferedReader取得しましたが、特定の文字列で特定の値を取得する必要があります。

これはテキストです:

    aimtolerance = 1024;
    model = Araarrow;
    name = Bow and Arrows;
    range = 450;
    reloadtime = 3;
    soundhitclass = arrow;
    type = Ballistic;
    waterexplosionclass = small water explosion;
    weaponvelocity = 750;

        default = 213;
        fort = 0.25;
        factory = 0.25;
        stalwart = 0.25;
        mechanical = 0.5;
        naval = 0.5;

default=;の間の正確な数を取得する必要があり ます。

これは「213」です

4

5 に答える 5

3

このようなもの....

String line;
while ((line = reader.readLine())!=null) {
   int ind = line.indexOf("default =");
   if (ind >= 0) {
      String yourValue = line.substring(ind+"default =".length(), line.length()-1).trim(); // -1 to remove de ";"
      ............
   }
}
于 2012-07-20T15:27:29.620 に答える
0

最終結果だけを気にする場合、つまり'='で区切られた値のテキストファイルからデータを取得する場合は、組み込みのPropertiesオブジェクトが役立つと思いますか?

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

これはあなたが必要とすることの多くを行います。もちろん、これを手動で実行したい場合は、適切なオプションではない可能性があります。

于 2012-07-20T15:24:40.963 に答える
-1

「default=」で文字列を分割し、indexOfを使用して「;」の最初の出現箇所を見つけます。0からインデックスまでの部分文字列を実行すると、値が得られます。

http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlを参照してください

于 2012-07-20T15:23:43.060 に答える
-1

正規表現の使用:

private static final Pattern DEFAULT_VALUE_PATTERN
        = Pattern.compile("default = (.*?);");

private String extractDefaultValueFrom(String text) {
    Matcher matcher = DEFAULT_VALUE_PATTERN.matcher(text);
    if (!matcher.find()) {
        throw new RuntimeException("Failed to find default value in text");
    }
    return matcher.group(1);
}
于 2012-07-20T15:44:50.683 に答える
-1

Propertiesクラスを使用して文字列をロードし、そこから任意の値を見つけることができます

String readString = "aimtolerance = 1024;\r\n" + 
"model = Araarrow;\r\n" + 
"name = Bow and Arrows;\r\n" + 
"range = 450;\r\n" + 
"reloadtime = 3;\r\n" + 
"soundhitclass = arrow;\r\n" + 
"type = Ballistic;\r\n" + 
"waterexplosionclass = small water explosion;\r\n" + 
"weaponvelocity = 750;\r\n" + 
"default = 213;\r\n" + 
"fort = 0.25;\r\n" + 
"factory = 0.25;\r\n" + 
"stalwart = 0.25;\r\n" + 
"mechanical = 0.5;\r\n" + 
"naval = 0.5;\r\n";
readString = readString.replaceAll(";", "");
Properties properties = new Properties();

System.out.println(properties);
try {
    properties.load(new StringReader(readString));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(properties);

String requiredPropertyValue = properties.getProperty("default");
System.out.println("requiredPropertyValue : "+requiredPropertyValue);
于 2012-07-21T17:50:09.407 に答える