-1

I'm getting the Syntax errors "Syntax error on token "{", { expected after this token" and "Syntax error, insert "}" to complete Block" after I use the super() keyword in my Customer class. I've looked around on other forum posts concerning this but I already have super() as the first thing in my Customer class. How do I fix this?

import java.util.*;

public class Main 
{
public class Customer 
{
   public String Name = "";
   public int ID = 0000;
   public int plansLeft = 0;


   public Customer()
    {
    }

   public Customer (String n, int id, int pl) 
   {
      Name = n;
      ID = id;
      plansLeft = pl;
   }
   public void setName(String n)
   {
      Name = n;  
   }
   public void setidnumber(int id)
   {
       ID = id;
   } 
   public void setdepartmen(int pl)
   {
       plansLeft = pl;
   }


   public String getName()
   {
      return Name;
   }
   public int getIDNumber()
   {
      return ID;
   }
   public int getPlansLeft()
   {
      return plansLeft;
   }
}


public class Session extends Customer 
{
    super(Name, ID, plansLeft);    // <------------------------- issue is here

    public int sessionIDNumber = 0000;
    public int spotsLeft = 0;

    public Session()
    {
    }

    public Session (int cID, int sl) 
       {
        sessionIDNumber = cID;
        spotsLeft = sl;
       }

    public int getsessionIDNumber() {
        return sessionIDNumber;
    }

    public int getspotsLeft() {
        return spotsLeft;
    }

    public void setsessionIDNumber(int sessionIDNumber) {
        this.sessionIDNumber = sessionIDNumber;
    }

    public void setspotsLeft(int sl) {
        this.spotsLeft = sl;
    }
}




public static void main(String[] args) 
{
    Session session = new Session(0117, 1);
    Customer customer = new Customer("Sarah", 12345678, 12);

    System.out.println("Customer Name: " + customer.getName() + "\nCustomer ID: " + customer.getIDNumber() + "\nLessons Remaining: " + customer.getPlansLeft());
    System.out.println("\nSession ID: " + session.getsessionIDNumber() + "\nOpen Spots Available: " + session.getspotsLeft());
}
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Ryan Horner
  • 123
  • 1
  • 1
  • 7
  • 2
    **not** first line of *class*, first line of *constructor* – user85421 Aug 28 '18 at 01:46
  • Why is this marked as duplicate when the linked post doesn't have the same error message as me? These responses have just confused me more than I was already and now my question won't get as many views/answers anymore. – Ryan Horner Aug 28 '18 at 02:21
  • 1
    You don't need any more views. The duplicate explains how to use super and that is your problem and the reason for the error -- you don't know. And your question has already been answered – Hovercraft Full Of Eels Aug 28 '18 at 02:27

2 Answers2

2

You have to pass some value to super class constructor. And you should call super as first line after constructor

It should look something like this

public Session (int cID, int sl, String Name, int id, int plansLeft) 
   {
    super(Name, id, plansLeft);
    sessionIDNumber = cID;
    spotsLeft = sl;
   }

Main

public static void main(String[] args) 
{

Customer customer = new Customer(0117, 1, "Sarah", 12345678, 12);

System.out.println("Customer Name: " + customer.getName() + "\nCustomer ID: " + customer.getIDNumber() + "\nLessons Remaining: " + customer.getPlansLeft());
System.out.println("\nSession ID: " + customer.getsessionIDNumber() + "\nOpen Spots Available: " + customer.getspotsLeft());
}
}
bakero98
  • 805
  • 7
  • 18
0

You want:

public Session() { 
  super(Name, ID, plansLeft);
}

super() as a call to the parent constructor must be the first line of a child constructor.

moilejter
  • 958
  • 8
  • 9