我在使用嵌套的`if`语句时遇到了麻烦

时间:2012-08-06 17:52:46

标签: java if-statement

我正在解析一个可能有日期的文件。如果日期存在,它的格式为两位数或四位数年份,但始终使用斜杠(例如,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这样的语句,而且我在解密错误方面遇到了麻烦。第9行(第一行)的else在编译期间给出了else without if错误。我怀疑我在某个地方有一个错位的大卷曲,虽然根据我引用的示例代码和教程似乎没问题。我无法发现问题,而且我的“试试看看会发生什么”的实验都没有成功。有人能指出我没看到的东西吗?

4 个答案:

答案 0 :(得分:7)

这不会编译为:

if (inputLine.lastIndexOf.isNumeric("/")+3);

在各种方面都是错的 - me 你的意思甚至不清楚。 lastIndexOf是一个方法调用 - 为什么你像字段一样使用它?你想把3加到什么?您希望何时将条件评估为真?

然后你的if / else语法错了。您通常应该使用:

if (condition) {
    // Code
} else {
    // Code
}

相反,你有:

if (condition); {
   // Code
else
   // Code
}

注意你有这个坏支撑的额外分号。关键是要指定:

  • 条件
  • 条件为真时执行的代码块
  • 要执行的代码块

这些块中的每个都必须在{ }之内,除非它是单个语句。

虽然可以在某些情况下没有大括号,但通常始终使用它们通常会更好。当您将单个语句主体更改为多语句主体时,错误的空间就会减少。

答案 1 :(得分:3)

在if语句的条件之后有一个分号,它有效地终止了if语句的主体。

if (inputLine.lastIndexOf.isNumeric("/")+3)/*;*/

此外,inputLine是什么类型的对象?它似乎没有用类型声明。我假设String,在这种情况下没有名为“lastIndexOf”的字段。

if-without-else是您的代码的几个问题之一。我建议查看其他代码示例以学习Java语法。

答案 2 :(得分:0)

正如@theglauber所说,用正则表达式解析这种输入看起来要容易得多。

String inputLine = "foo foo foo foo foo 10/26/2013 bar bar bar bar bar";
Pattern p = Pattern.compile(".*(\\d{2}\\/\\d{2}\\/\\d{4}).*");
Matcher m = p.matcher(inputLine);
if (m.matches()) {
    String taskDate = m.group(1);
    System.out.println(taskDate + " " + inputLine);
}

此外,这个if-then if-then-else Java教程可能对您有所帮助。如果你使用正则表达式,请阅读this

答案 3 :(得分:0)

好。除了额外的分号之外,你的代码中缺少花括号,你有这个:

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(); }  //
}

何时应该:

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();  //
}

知道你是否有未闭合的花括号或圆括号的一种方法是使用文本编辑器(如Notepad ++)或IDE(如Netbeans)突出显示打开的花括号结束的位置。但老实说,你似乎缺乏对Java编程的基本理解,所以我建议你阅读一本关于语言的书(比如Deitel的How to program或类似的)来获得它的基础知识。

相关问题