Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: CommaND Prompt:class, interface, or enum expected

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default CommaND Prompt:class, interface, or enum expected

    i am a beginner. i want print a number on the system with this code:

    int myNum=33 ;
    System.out.println(myNum) ;

    command prompt rather put this out:


    C:\Users\HP\Documents>javac myNum.java
    myNum.java:1: error: class, interface, or enum expected
    int myNum=33 ;
    ^
    myNum.java:1: error: class, interface, or enum expected
    int myNum=33 ;
    ^
    myNum.java:2: error: class, interface, or enum expected
    System.out.println(myNum) ;
    ^
    3 errors

    pls, are these errors from my code or whaT?
    Last edited by Chinedu & ICT; December 27th, 2019 at 05:08 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    All executable statements must be inside of a method that is in a class.

    The code that was posted does not show where the statements with the errors are located.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    Quote Originally Posted by Norm View Post
    All executable statements must be inside of a method that is in a class.

    The code that was posted does not show where the statements with the errors are located.
    I made myNum into a class MyNum, and rewrote the code to:
    public class MyNum {
    int MyNum=147;
    System.out.println( MyNum ) ;
    }

    when i compiled the above code, this is the output:


    C:\Users\HP\Documents>javac MyNum.java
    MyNum.java:3: error: <identifier> expected
    System.out.println( MyNum ) ;
    ^
    MyNum.java:3: error: <identifier> expected
    System.out.println( MyNum ) ;
    ^
    2 errors

    please is my code right? I'm working on the identifier.
    Last edited by Chinedu & ICT; December 28th, 2019 at 05:20 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    The code must be inside of a method.
    https://docs.oracle.com/javase/tutor...O/methods.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: CommaND Prompt:class, interface, or enum expected

    Quote Originally Posted by Norm View Post
    The code must be inside of a method.
    https://docs.oracle.com/javase/tutor...O/methods.html
    problem solved. thank you. this is the code that was compiled:

    public class MyNum {
        public static void main(String[] args) {
           int MyNum=147;
               System.out.println( MyNum ) ;
        }
    }

    cmd.exe:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\HP>cd Documents

    C:\Users\HP\Documents>javac MyNum.java

    C:\Users\HP\Documents>java MyNum
    147

    C:\Users\HP\Documents>

    but please in the former code i used
    public class MyNum {
        int MyNum=147;
           System.out.println(MyNum);
    }

    cmd.exe output:Identifier expected.
    which identifier is missing in the codelines?
    Last edited by Chinedu & ICT; December 28th, 2019 at 05:42 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    The method definition was missing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    Quote Originally Posted by Norm View Post
    The method definition was missing.
    how? i don't understand.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    The code with the error did not have a method to contain the two statements.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    Quote Originally Posted by Norm View Post
    The code with the error did not have a method to contain the two statements.
    a method, as in
    public static void main(String[] args)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    Yes that is how a method is declared. Did you go to the link I posted?

    https://docs.oracle.com/javase/tutor...O/methods.html
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Chinedu & ICT (December 29th, 2019)

  12. #11
    Junior Member
    Join Date
    Dec 2019
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    yep! viewing it.am starting from declaring classes.



    whats ds trackback input field for?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CommaND Prompt:class, interface, or enum expected

    Where did you see that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Class, Interface, Enum expected.
    By PizzaRolls in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 13th, 2013, 09:57 AM
  2. class, interface, or enum expected errors??
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2013, 05:45 AM
  3. Class, Interface, or Enum expected
    By SilvioSpeed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2012, 09:34 AM
  4. class,interface, or enum expected?
    By Eimaj in forum What's Wrong With My Code?
    Replies: 21
    Last Post: September 28th, 2012, 03:16 PM
  5. [SOLVED] Error: Class,interface or enum expected
    By FJIW in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2011, 03:08 PM