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: <Identifier Expected> - Constructor method?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default <Identifier Expected> - Constructor method?

    Hello,

    I am receiving an error when I try to create a constructor method. It makes no sense at all to me because right next to the program, I have a correctly compiled version of it. What is identifier expected?



    What am I doing wrong?
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,665
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: <Identifier Expected> - Constructor method?

    Please copy the full text of the error message and paste it here. Also post the code(in code tags).
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: <Identifier Expected> - Constructor method?

    Did you see the attached image?

    Full error in BlueJ:

    The symbol namd in the error message was expected to appear at this point in the code. It was not there; instead, there was some other symbol. Try to think about why this symbol may be expected here>



    Correct version:

      class Rectangle2
       {
          private double length, width;
          public Rectangle2(double l, double w)     // put instance variables inside constructor first
          {
             length = l;
             width = w;
          }
          public double area()
          {
             return length*width;
          }
          public Rectangle2 copy()
          {
             return new Rectangle2(length, width);
          }
       }



    Version that won't compile

    class Rectangle3
    {
        private double length, width;
        public Rectangle3(double 1, double w)         // error at this line
        {
            length = 1;
            width = w;
        }
     
        public double area()
        {
            return length*width;
        }
     
        public Rectangle3 copy()
        {
            return new Rectangle3(length, width);
        }
     
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 300 Times in 282 Posts

    Default Re: <Identifier Expected> - Constructor method?

    In the one that won't compile, is that length variable an l or a 1 (one)?
    It looks like a 1.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    TSSF44 (October 28th, 2013)

  6. #5
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: <Identifier Expected> - Constructor method?

    Quote Originally Posted by aussiemcgr View Post
    In the one that won't compile, is that length variable an l or a 1 (one)?
    It looks like a 1.
    WOW. Silly mistake. Although they do look pretty similar.

    Thank you for finding it.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,665
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: <Identifier Expected> - Constructor method?

    That is a good example why I don't like images. If you do a Find for 1 in the posted source it finds the mistake.
    No way to do a Find with an image.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. error Identifier expected?
    By chandresh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 15th, 2012, 02:34 AM
  2. ok i have tried everything but identifier expected keeps popping up!
    By knoxy5467 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 5th, 2011, 11:13 PM
  3. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 01:50 PM
  4. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 08:33 PM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM