日付が含まれている可能性のあるファイルを解析しています。日付がある場合は、2 桁または 4 桁の年でフォーマットされますが、常にスラッシュが付きます (例: MM/DD/YY または MM/DD/YYYY)。
public class T2 {
public static void main(String args[]) {
inputLine = "foo foo foo foo foo 10/26/2013 bar bar bar bar bar";
if(inputLine.indexOf("/")>0) { // if date exists
taskDateSOF = (inputLine.indexOf("/")-2); // then start of field is first instance of / minus two
if (inputLine.lastIndexOf.isNumeric("/")+3); { // and if there's a number in the third position after the last /
taskDateEOF = (inputLine.lastIndexOf("/") + 4); // the the end of date field is last instance of / +4
else // <<< this "else" give an "else without if compiler error
taskDateEOF = (inputLine.lastIndexOf(("/" + 2))); // else it's the last instance of / +2
taskDate = inputLine.trim().substring(taskDateSOF,taskDateEOF).trim(); } //
}
else
taskDate = "00/00/0000";
}
System.out.println(taskDate+" "+inputLine);
}
}
少し苦労した後、私は以前にこのような if ステートメントをネストしたことがなく、エラーを解読するのに苦労していることに気付きました。else
9 行目 (最初の行) で、else without if
コンパイル中にエラーが発生します。私が参照したサンプル コードとチュートリアルに基づくと、問題ないように見えますが、中かっこの位置がどこか間違っていると思われます。私は問題を見つけることができず、私の「試してみて何が起こるか見てみよう」という実験はどれも成功していません。誰かが私が見ていないものを教えてもらえますか?