建议解决Java中的语法错误

时间:2011-09-21 09:10:50

标签: java

我对Java非常缺乏经验,我花了百分之九十的时间来修复程序中的错误。此外,当我遇到语法错误时,我发现它们并修复它们是非常困难的。如果有经验的程序员可以提供一些交易提示,我将非常感激。我的类型错误示例是令牌“void”,@ expected 上的语法错误。在public void employeeName()。

import java.util.Scanner;

public class EmployeeDriver {

String employeeName;
public void employeeName() {
   try {

        Scanner scannerName = new Scanner(System.in);
        System.out.println("Employye Name " + scannerName.nextLine());

        System.out.println("When did you hire the Employee");
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("The Employee name is " + scanner1.nextLine());

        System.out.println("What is the Employee's Number");
        Scanner scannerNum = new Scanner(System.in);
        System.out.println("The Employee name is " + scannerNum.nextLine());

    }catch (Exception e){
        System.out.println("Error occurred" + e.getMessage());
    }

}

public static void main(String[] args) {

    System.out.println("What is the Employee name");
    EmployeeDriver namingEmployeeObject = new EmployeeDriver();
    namingEmployeeObject.employeeName();
    Scanner scanner = new Scanner(System.in);
}

}

3 个答案:

答案 0 :(得分:6)

嗯,在这种情况下,问题是你试图在另一个方法(employeeName)中声明一个方法(main)。你不能这样做。

至于如何快速查找和修复此类错误:经常保存和构建 。如果您正在使用Eclipse,那么一旦保存就会编译,我怀疑其他IDE也会这样做。

这样,只要你遇到问题,你就可以记得自上次代码编译以来你所做的事情,并找出问题所在。说实话,编程器通常通常非常有用。

答案 1 :(得分:2)

我可能会从反IDE社区中获得很多好处,但我建议你去下载IntelliJ IDEA的免费“社区版”。这是一个非常灵活的Java IDE,可以为您进行实时语法高亮显示。您将能够看到代码的哪些部分具有不正确的语法,以及原因。

IntelliJ IDEA Download

An example of the Code Inspection features of IDEA

祝你好运。

答案 2 :(得分:1)

请尝试给出代码:

import java.util.Scanner;

public class EmployeeDriver {
    String employeeName;

    public void employeeName() {
        try {
            Scanner scannerName = new Scanner(System.in);
            System.out.println("Employye Name " + scannerName.nextLine());
            System.out.println("When did you hire the Employee");
            Scanner scanner1 = new Scanner(System.in);
            System.out.println("The Employee name is " + scanner1.nextLine());
            System.out.println("What is the Employee's Number");
            Scanner scannerNum = new Scanner(System.in);
            System.out.println("The Employee name is " + scannerNum.nextLine());
        } catch (Exception e) {
            System.out.println("Error occurred" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println("What is the Employee name");
        EmployeeDriver namingEmployeeObject = new EmployeeDriver();
        namingEmployeeObject.employeeName();
        Scanner scanner = new Scanner(System.in);
    }
}