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: Beginner code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner code

    Hi everyone,

    I'm just starting to learn Java. I copied the following program from a textbook, but it doesn't seem to work for some reason. I'd appreciate your help.

    Here's the code:


    public class Hello {
    public static void main(String[] args) {
    System.out.println("Hello, world!");
    }
    }


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

    Default Re: Beginner code

    Please copy and paste here the full text of any error messages you get. Explain what you have done and what happens when you do it.

    Have you read the tutorial that explains how to compile and execute java programs?
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    Click on the link for the system you are on.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    what kind of IDE are you using?
    if you are using notepad, I suggest you download an IDE such as Eclipse online. They have very easy tutorials on how to use them, and it makes programming much easier.
    Here's a link: Eclipse Downloads
    The second download is suitable for most programmers and students

    other than the IDE, I don't see anything wrong with the code.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Beginner code

    if you are using notepad, I suggest you download an IDE such as Eclipse online. They have very easy tutorials on how to use them, and it makes programming much easier.
    If one is a beginner, as the original post would suggest, I would hesitate to recommend this advice. While IDE's do make things easier for more advanced programmers, there are many concepts which become hidden through an IDE and can only be learned by command line programming. Plus, this is suggestive that an IDE will solve the original issue, which we don't even know what it is.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    Seems like you havn't specified what package it should be in.

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

    Default Re: Beginner code

    @djl1990 Classes do not have to be in packages.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of any error messages you get. Explain what you have done and what happens when you do it.

    Have you read the tutorial that explains how to compile and execute java programs?
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    Click on the link for the system you are on.
    Hi Norm. Here is the error message I received:
    xception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - class Hello is public, should be declared in a file named Hello.java
    at Hello.<clinit>(JavaApplication5.java:1)
    Java Result: 1

    I'll take a loot at the tutorial. I got the original program from a class textbook. I can't take the class right now, so I am reading the text as the next best option.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    Yes, I am very much a beginner. I got the code from a book I bought: Building Java Programs: A Back to Basics Approach, 2nd edition
    by Stuart Reges and Marty Stepp.

    Also, I downloaded BlueJ. I was able to get the code compiled, but couldn't get it to run. I apologize, but I'm still learning the definition of such terms as class and package as they relate to Java.

    I know you're busy, so I appreciate your help. Java looks like a lot of fun!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner code

    class Hello is public, should be declared in a file named Hello.java
    What is the name of the .java file? The compiler says what the problem is. Name the file: Hello.java

    I was able to get the code compiled
    The compile was not successful. The above is a compiler error message.
    You need to get rid of compiler errors before you can run the code.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    Quote Originally Posted by Norm View Post
    What is the name of the .java file? The compiler says what the problem is. Name the file: Hello.java


    The compile was not successful. The above is a compiler error message.
    You need to get rid of compiler errors before you can run the code.
    -------------

    How do I name the file?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner code

    How do I name the file?
    That depends on what OS you are on. Most OSs have a rename command. Find that and use it to rename the file. On windows, right click the file and select the Rename option. Type the new name over the old one.
    Or edit the existing file and save it with the new name.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner code

    Thanks for the help. I talked to a teacher at a local community college. She recommended the book Building Java™ Programs: A Back to Basics Approach, 2/e It looks good. I'm going to use BlueJ for the IDE. I finally found what looks like a good tutorial on how to use it:Creating and Running programs in BlueJ. I wish I could take a class on Java so I could talk to someone face-to-face, but that won't work with my schedule right now. Still, this forum looks good. I appreciate everyone's help.

Similar Threads

  1. Hello, I'm a beginner and I really need help for my code..
    By aimEhjO in forum Member Introductions
    Replies: 10
    Last Post: August 4th, 2012, 09:22 AM
  2. Beginner having some trouble with two parts in my code
    By csprung in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 04:23 AM
  3. Need Help with code ...beginner
    By Envious1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2012, 08:52 AM
  4. (beginner) if else code.
    By dumb1101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2012, 02:47 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM