Syntax error on token(s), misplaced construct(s) Code Help Needed

前端 未结 2 1093
无人及你
无人及你 2021-01-23 18:03

I use eclipse to help me code & I have been having issues with the error message \"Syntax error on token(s), misplaced construct(s)\" coming up, I\'m not entirely sure what

相关标签:
2条回答
  • 2021-01-23 18:31

    public static void main(String args[]) is a method it needs to create a block with curly braces. It doesn't contain the block in the ThreadsUnitProject1 class.

    public static void main(String args[]){}
    

    Also the import statements should be outside the class declaration.

    Full Example

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class ThreadsUnitProject1 {
    
        public static void main(String args[]) {
        }
    
        class InvalidAgeException extends Exception {
            private static final long serialVersionUID = 1L;
    
            public InvalidAgeException() {
                super("The age you entered is not between 0 and 125");
            }
        }
    
        class QuestionOne extends Thread {
            public void main(String args[]) {
                System.out.println("What is your name?");
    
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        System.in));
                String name = "";
    
                try {
                    name = br.readLine();
                } catch (IOException e) {
                    System.out.println("Error: " + e);
                    System.exit(1);
                }
    
                System.out.println("Hello " + name + ", how old are you?");
    
                String i;
                int age;
    
                try {
                    i = br.readLine();
                    age = Integer.valueOf(i);
                } catch (IOException e) {
                    System.out.println("Error: " + e);
                    System.exit(1);
                } finally {
                    System.out.println("No errors found.");
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-23 18:50

    Use {} after public static void main(String args[]), not ;.

    0 讨论(0)
提交回复
热议问题