简体   繁体   中英

Advise on tackling syntax errors in Java

I very inexperience with Java, I'm spending ninety percent of time fixing errors in my program. Also when I experience a syntax error, it's super hard for me to spot them and fix them. I'll be very thankful if a experienced programmer can give some tips of the trade. An example of a type error for me is Syntax error on token "void", @ expected . On 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);
}

}

Well, in this case the problem is that you're trying to declare one method ( employeeName ) within another ( main ). You can't do that.

As for how to find and fix such errors quickly: save and build often . If you're using Eclipse, that will compile as soon as you save, and I suspect other IDEs do the same.

That way, as soon as you've got a problem, you can hopefully remember what you've done since the code last did compile, and work out what's wrong. To be honest, compilers are usually pretty helpful these days.

I'm probably going to get a lot of flack from the anti-IDE community, but I recommend you go download the free "Community Edition" of IntelliJ IDEA. This is a really slick Java IDE that will do real time syntax highlighting for you. You will be able to see what parts of the code have incorrect syntax, and why.

IntelliJ IDEA Download

An example of the Code Inspection features of IDEA

Good luck.

Please try given code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM