错误:'。class'预期

时间:2013-01-01 11:51:05

标签: java class

嗨朋友们在编译以下错误时即将到来

错误:'。class'预期

我无法找到错误。我检查了很多网站,但无法找到为什么会出现此错误。 Plz帮助

提前致谢。

import java.io.*;

class Test
{
    boolean isGoodEmployee(boolean ismarried,int noofchild,String middlename,String childnames[])
    {
        boolean child,letter,last,child;

        if (noofchild <= 2)
            child=true;
        boolean firstletter = middlename.startsWith("k");
        boolean lastletter = middlename.endssWith("e");

        if (firstletter && (!lastletter))
            letter = true;

        int lastnameletterscount = lastname.length();

        if (lastnameletterscount > 4)
            last = true;

        String name = raju;

        for (int i=0; i < noofchild; i++)
            if(name.equalsIgnoreCase(childnames[i]))
            {
                child = true;
                break;
            }
    }
}


class GoodEmployee
{
    public static void main(String args[]) throws IOException
    {
        String firstname, middlename, lastname;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the first name of employee");
        firstname = br.readLine();
        System.out.println("enter the middle name of employee");
        middlename = br.readLine();
        System.out.println("enter the last name of employee");
        lastname = br.readLine();
        //System.out.println("full name of employee is:"+first+middle+last);
        System.out.println("enter employee is married or not");
        boolean ismarried = Boolean.parseBoolean(br.readLine());

        if (ismarried)
            System.out.println("enter number of childrens");

        int noofchild = Integer.parseInt(br.readLine());
        String childnames[] = new String[noofchild];
        System.out.println("enter children names");

        for (int i=0; i < noofchild; i++)
            childnames[i] = br.readLine();
        Test t = new Test();
        t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
    }
}

我正在使用“javac GoodEmployee.java”来编译程序

4 个答案:

答案 0 :(得分:4)

您在isGoodEmployee中遇到了错误匹配的方法调用。

childnames参数被定义为一个数组,所以你可以直接传入参数,替换:

t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);

t.isGoodEmployee(ismarried,noofchild,middlename,childnames);

除此之外,不要忽略其他编译器错误,例如

middlename.endssWith("e");
               ^---------extra 's'

熟悉javadocs

答案 1 :(得分:1)

这段代码有很多错误:
你已经在isGoodEmployee()(第一行)中两次声明了布尔子项 endssWith应该是endsWith()
等等,但不是你说的问题。你用什么命令行来编译这个类?

答案 2 :(得分:0)

@Reimeus已经钉了它。

奇怪的编译错误可以解释如下。在这一行:

 t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);

childnames[]短语应该是Java表达式。但实际上,语法最类似于一个类型......实际上是一个名为childnames的某个(不存在的)类的数组。 Java编译器的语法错误恢复代码已决定,如果它在类型名称(即.class)之后插入childnames[].class,将提供语法上有效的表达式。

当然,编译器建议的更正是没有意义的......尤其是因为在您的应用程序中不会有名为childnames的类或接口。


故事的寓意是,不寻常的语法错误有时会导致编译器产生特殊的错误消息,只有在你能弄清楚解析器如何解析错误的输入时才有意义。

答案 3 :(得分:0)

如果您使用的是IDE,则使用main方法创建类。 如果您尝试使用命令窗口运行它,我不认为编译需要'.class'文件,而是需要执行它。因此,通过输入javac GoodEmployee获得.class文件并且没有错误输入java GoodEmployee,它将执行您拥有的.class文件。

相关问题