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

Thread: constructor method syntax error

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default constructor method syntax error

    Newbie here. Sorry if this is silly.

    I am getting 'Not a statement, ; expected error' for the second constructor method below.
    Can't figure the problem.

    class Knapsack {

    public int size;
    public String[] items;

    public Knapsack(int size){
    size = 5;
    }

    public Knapsack(String[] items){
    items[] = {"doer", "kicker", "pusher"};
    }

    public String getItem(int i) {
    return items[i];
    }

    public boolean isFull(int size) {
    if (size >= 7) {
    return true;
    }
    return false;
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: constructor method syntax error

    Can you put a comment on the statement with the problem so there is no confusion?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor method syntax error

    Thank you

    Newbie here. Sorry if this is silly.

    I am getting 'Not a statement, ; expected error' for the second constructor method below.
    Can't figure the problem.



     
    class Knapsack {
     
        public int size;
      public String[] items;
     
        public Knapsack(int size){
        size = 5;
      }
     
        public Knapsack(String[] items){
        items[] = {"doer", "kicker", "pusher"}; //The error I get for this line says - "Not a statement, ; expected"
      }
     
      public String getItem(int i) {
      return items[i];
      }
     
        public boolean isFull(int size) {
        if (size >= 7) {
          return true;
        }
            return false;
      }
    }
    Last edited by bertu; February 16th, 2022 at 12:52 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: constructor method syntax error

    I am getting 'Not a statement, ; expected error'
    Please add a comment on the actual statement that gives that error message.
    Also it helps if the full text of the error message is copied and posted to show the source line with the error and the location on that line where the error was found.
    public Knapsack(String[] items){
    Is this the statement with the error? I don't see any problem with that statement.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor method syntax error

    Norm
    Sorry again for missing the comment.
    I have edited the code section with a comment.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: constructor method syntax error

    Ok, thanks.
    The syntax for assigning an array value to an array variable requires the variable on the left of the =
    and an array value to the right of the =.
    items[] is not an array variable - remove the []s
    {"doer", "kicker", "pusher"} is not an array value. Change by adding: new String[] before the {

    Also there is a problem with the two variables named: items. There are two declarations of the variable: one in the class and one in the method. To access the one in the class, prefix the variable with "this."
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor method syntax error

    Thank you.
    As you suggested, when I corrected the code as below, the error has disappeared.

    items = new String[] {"doer", "kicker", "pusher"}

    One question:
    Why did you say {"doer", "kicker", "pusher"} is not an array value?
    I see this as example from other websites.
    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: constructor method syntax error

    Yes that is allowed when the array is being declared on the same statement.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2022
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructor method syntax error

    Thanks

Similar Threads

  1. Replies: 1
    Last Post: November 26th, 2021, 08:48 AM
  2. syntax error
    By juliaannn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 1st, 2014, 03:40 AM
  3. Syntax error!!!
    By Gerock7 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: April 22nd, 2014, 07:14 PM
  4. Syntax error
    By Kez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2014, 04:48 PM
  5. Syntax in method
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: September 23rd, 2011, 03:01 PM