Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: creating objects in java

  1. #1
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default creating objects in java

    Hi i am just new in java studying towards a certification.Currently am studying class fundamentals .Tried to do the following program from a book .When i run it its says
    Line 18 class, interface, or enum expected.My line 18 is this statement: import java.util.Scanner;

    Could someone assist.

    Thank you

    class GradeBookTest
    {
    public void displayMessage(String courseName)
    {
    System.out.println("Welcome to grade book for \n%s!\n,coursename" );
    }
    }

    import java.util.Scanner;
    class GradeBookTest1 {

    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    GradeBookTest myGradeBook=new GradeBookTest();
    System.out.println("Please enter the course name");
    String nameOfCourse=input.nextLine();
    System.out.println();
    myGradeBook.displayMessage(nameOfCourse);

    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: creating objects in java

    Pleas use [code] tags [/code] when posting code.

    Did the code in the book look like that, with two class declarations with the same name?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: creating objects in java

    no it is not exactjy like that.First the book was ilustrating creating a class and object and then there was the Main code.so i just compined the two codes thinking it will run.The book is "Java How to programm 8th edition "by Deitel

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: creating objects in java

    I assume the book explained how code is structured?
    I would suggest re-reading that section.
    Especially what a normal Java source file looks like and contains.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: creating objects in java

    i am currently looking at the book and i seem to be blank what it means to say "class, interface, or enum expected".

    could you help

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: creating objects in java

    Forget about the compiler messages, as the code is so wrong in terms of structure the compiler is at a complete loss as to what it is you are trying to do.
    Reread about the structure of a Java file and classes and compare that structure to what you have given the compiler to work with.
    jwolf likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: creating objects in java

    Quote Originally Posted by Tolls View Post
    Did the code in the book look like that, with two class declarations with the same name?
    You need glasses.

    kind regards,

    Jos

    ps. @OP: an import statement should be at the start of your source file.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: creating objects in java

    Quote Originally Posted by JosAH View Post
    You need glasses.
    Gah!
    How do people expect my ageing eyes to spot that...bah, grumble.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: creating objects in java

    Thanx for your contribution.If i am to remove the statements
    "import java.util.Scanner",
    Scanner input=new Scanner(System.in);
    String nameOfCourse=input.nextLine();
    and replace the statement:
    System.out.println("Please enter the course name");
    with:
    System.out.println("Name of the course"+nameOfCourse);
    and i declare String:
    String nameOfCourse="Java Programming";
    This program runs perfectly.
    What i will have simply done is assigning the String instead of inputing nameOfCourse from the keyboard.My question is how can i use this code to input the nameOfCourse from the keyboard?

    Thanx

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: creating objects in java

    Put all the code back the way it was, then stick the import statement at the top of the source file.

    The structure of a source file is:
    package statement (the package the classes in the file belong to).
    import statements (all import used by the classes).
    class declarations.

    This is why I said you need to read the bit about the structure of a java file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: creating objects in java

    thanx let me work on it

  12. #12
    pp100 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: creating objects in java

    Got it.simply put the main at the top and the class that i created at the bottom.Not so sure whether its a mere coicidence but the the program is now running.

    thanx for yo input

Similar Threads

  1. Creating Objects within While Loops
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 01:19 AM
  2. Creating a list with objects
    By thine_iniquity in forum New To Java
    Replies: 6
    Last Post: 03-25-2011, 04:55 AM
  3. Creating Objects from txt file?
    By Bread in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-11-2011, 08:40 PM
  4. Creating Java objects using "new"
    By linking90 in forum New To Java
    Replies: 1
    Last Post: 07-04-2010, 12:38 AM
  5. Creating and using java objects at runtime...
    By baran_khan in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2008, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •