Question

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

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 is wrong with my code.

The goal of this code is to write a program where a user enters their name and age and the program checks to see the age is between 0 and 125. If not, the program shows an error code (use Exception Class)

Here is my current code: Errors are showing up in lines 1 and 4

public class ThreadsUnitProject1 {
    import java.lang.String;
    import java.io.*;

    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);
            }
            catch(InvalidAgeException e) {
                System.out.println("Error: " + e);
                System.exit(1);
            }
            finally {
                System.out.println("No errors found.");
            }
        }
    }
}

Thank you thank you thank you for all of your help, I have been coding for awhile, but I’m new to Java.
Thanks again!
-Kristen

Enquirer: Kristen

||

Solution #1:

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.");
            }
        }
    }
}
Respondent: Kevin Bowersox

Solution #2:

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

Respondent: Ça?atay Demircan

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy