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 6 of 6

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code?

    I am a very new beginner, I mean I almost seriously started yesterday. I have the Java All In One For Dummies 9 Books In One , and in the first book, but I am confused. If there is anybody out there to provide regular help and clarification, please, but all means, contact me. But here is my first code, which still can't compile even though its word for word from the book.
    public class HelloApp
    {
    public static void main(String [] args)
    {
    helloMessage = "Hello, World!" ;
    System.out.println(helloMeassge);


    }
    static String helloMessage;
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: What is wrong with my code?

    Quote Originally Posted by dannyboi View Post
    ...word for word from the book...
    Either the book has a misprint, or you didn't quite get it:
    .
    .
    .
            helloMessage = "Hello, World!" ;  //<---Spelling #1
            System.out.println(helloMeassge); //<---Spelling #2
    .
    .
    . 
       static String helloMessage; //<---Spelling #1

    Unlike the game of horseshoes (and unlike searches on Google), in Java, close doesn't count. Two out of three isn't good enough.

    Cheers!

    Z
    Last edited by Zaphod_b; July 22nd, 2012 at 03:41 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    Quote Originally Posted by Zaphod_b View Post
    Either the book has a misprint, or you didn't quite get it:
    .
    .
    .
            helloMessage = "Hello, World!" ;  //<---Spelling #1
            System.out.println(helloMeassge); //<---Spelling #2
    .
    .
    . 
       static String helloMessage; //<---Spelling #1

    Unlike the game of horseshoes (and unlike searches on Google), in Java, close doesn't count. Two out of three isn't good enough.

    Cheers!

    Z
    Ok then, I get the "message" part, but still I get:public class HelloApp
    {
    public static void main(String [] args)
    {
    helloMessage = "Hello, World!" ;
    System.out.println(helloMessage);


    }
    static String helloMessage;
    }

    and this :

    init:
    deps-jar:
    Compiling 1 source file to C:\Users\daniel\JavaApplication11\build\classes
    compile-single:
    run-single:
    java.lang.NoClassDefFoundError: javaapplication11/HelloApp
    Exception in thread "main"
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  4. #4
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    Maybe because the object helloMessage isn't declaired a String until the program is done.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What is wrong with my code?

    Quote Originally Posted by Firestar912 View Post
    Maybe because the object helloMessage isn't declaired a String until the program is done.
    Java actually doesn't care if you declare your variables below the code where they are used, so long as the scope is upward of their use.
    For example:

    public class Variable {
        String myVariable = "Hello World!";
        public someRandomBlockOfCode {
            //....
        }
        public tryMe{
            System.out.println(myVariable);
        }
    }
    ...is the same as:
    public class Variable {
        public someRandomBlockOfCode {
            //....
        }
        String myVariable = "Hello World!";
        public tryMe{
            System.out.println(myVariable);
        }
    }
    ...is the same as:
    public class Variable {
        public someRandomBlockOfCode {
            //....
        }
        public tryMe{
            System.out.println(myVariable);
        }
        String myVariable = "Hello World!";
    }
    However, the following code will not work, because the variable is not within scope, or a higher bracket than the variables use
    public class Variable {
        public someRandomBlockOfCode {
            String myVariable = "Hello World!";//Variable declared here
            //....
        }
        public tryMe{
            System.out.println(myVariable);//is not visible here
        }
    }
    Last edited by jps; July 23rd, 2012 at 02:53 PM. Reason: typo corrected

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: What is wrong with my code?

    Quote Originally Posted by Firestar912 View Post
    Maybe ....
    Why would you say that? Have you tested it?

    Bottom line: The code works just fine after the spelling typo on line 6 was corrected. The program compiles and executes perfectly on my systems if it is stored in a file named HelloApp.java. (I use command line javac and java.)

    I didn't respond further to the Original Poster because I don't know how he is compiling it (IDE or whatever) so I can't offer additional help, and don't see any value in my trying to guess...


    Cheers!


    Z

Similar Threads

  1. what is wrong with my code? need help
    By balmung123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 04:12 AM
  2. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 28th, 2012, 09:08 PM
  3. what is wrong with this code???plz help me :(
    By kanikapant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 17th, 2011, 06:03 PM
  4. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  5. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM

Tags for this Thread