3

I have an object called Customer, that I want to add into my ArrayList. The plan is to make a program where I can search for customers and click into their profile. This is the constructor for my object:

public Customer(String firstname, 
                String secondname, 
                LocalDate birthdate, 
                String mail, 
                String phonenmbr, 
                String adress,
                String postnumber, 
                String postplace)

And this is when I try to place to object into the ArrayList:

ArrayList<Customer> customerList = new ArrayList<Customer>();

customerList.add(
new Customer("AARON",
               "SMITH",
               LocalDate.of(1980,01,01),
               "[email protected]",
               "92081",
               "Testveien 109A",
               "1234",
               "OSLO"));

Multiple markers at this line

Syntax error on token ".", @ expected after this token

Syntax error on token ";", @ expected

Syntax error on token ";", @ expected

4
  • Constructors with many parameters like that are generally discouraged. You should consider having a constructor with essential information only, and getters and setters for the rest. Commented Mar 9, 2016 at 10:06
  • 1
    Post the code with the method inside or is it outside a method? Commented Mar 9, 2016 at 10:12
  • I posted the code inside a method, and it worked. Thank you! Commented Mar 9, 2016 at 10:16
  • Is your problem solved? If not please post your complete code where are you getting this error. Commented Mar 9, 2016 at 10:16

3 Answers 3

1

You have space between 'post' and 'place' at the end as part of constructor argument. That is not valid nomenclature in java.

If its supposed to be single field define it like post_place. If they are two, add type for place as well separated by comma.

Sign up to request clarification or add additional context in comments.

Comments

1

take a look to ...String post place) in the Costumer constructor... there is an space between name and parameter, and this is not allowed ..

Comments

0

Tutorial

For such basics in Java, study The Java™ Tutorials by Oracle.com, free of charge.

Wrong class syntax

You need curly-braces to define a class. You used parentheses.

Also, the word class is required.

package work.basil.example.classes;

import java.time.LocalDate;

public class Customer
{
    String firstname;
    String secondname;
    LocalDate birthdate;
    String mail;
    String phonenmbr;
    String adress;
    String postnumber;
    String postplace;
}

Or colocate in a single line some field members of the same type.

public class Customer
{
    String firstname, secondname, mail, phonenmbr, adress, postnumber, postplace;
    LocalDate birthdate;
}

Mark the member fields final if you intend for them to be shallowly-immutable. You will then need to define a constructor as well to populate the values.

public class Customer
{
    final String firstname, secondname, mail, phonenmbr, adress, postnumber, postplace;
    final LocalDate birthdate;

    public Customer ( final String firstname , final String secondname , final String mail , final String phonenmbr , final String adress , final String postnumber , final String postplace , final LocalDate birthdate )
    {
        this.firstname = firstname;
        this.secondname = secondname;
        this.mail = mail;
        this.phonenmbr = phonenmbr;
        this.adress = adress;
        this.postnumber = postnumber;
        this.postplace = postplace;
        this.birthdate = birthdate;
    }
}

Records

If the main purpose of your class is to transparently communicate shallowly-immutable data, define your class as a record.

For defining a record, we do use parentheses to define the member fields, similar to the code in the Question. A record also has a pair of curly-braces, left empty if you want default behaviors.

In a record, the compiler implicitly creates the constructor, getters, equals & hashCode, and toString.

public record Customer
        ( String firstname ,
          String secondname ,
          String mail ,
          String phonenmbr ,
          String adress ,
          String postnumber ,
          String postplace ,
          LocalDate birthdate )
{ }

Example usage

In modern Java, there is no need to repeat the generics type of the ArrayList. Use empty <> on the right side.

Also, generally best to use the highest-level of abstraction (super-class or super-interface) that meets your needs. So instead of referring to your list as an ArrayList, refer to it as a List, SequencedCollection, or Collection.

No need to include the word list in your variable’s name. You may well decide in the future to change to a different concrete class that is not a List, such as a SequencedSet like TreeSet. The plural customers will suffice.

SequencedCollection < Customer > customers = new ArrayList < > ( );

customers.add
        (
                new Customer
                        ( "AARON" ,
                                "SMITH" ,
                                "[email protected]" ,
                                "92081" ,
                                "Testveien 109A" ,
                                "1234" ,
                                "OSLO" ,
                                LocalDate.of ( 1980 , 1 , 1 )
                        )
        );

Where an unmodifiable list is appropriate, use List.of for convenient literals-style syntax.

SequencedCollection < Customer > customers =
        List.of
                (
                        new Customer
                                ( "AARON" ,
                                        "SMITH" ,
                                        "[email protected]" ,
                                        "92081" ,
                                        "Testveien 109A" ,
                                        "1234" ,
                                        "OSLO" ,
                                        LocalDate.of ( 1980 , 1 , 1 )
                                )
                );

Dump to console

You can print description of your objects, with text generated by the toString method.

Commonly used is either this:

System.out.println ( "customers = " + customers );

… or this:

customers.forEach ( System.out :: println );

Octal numbers

You wrote:

LocalDate.of(1980,01,01),

Do not add a leading zero to your int literals. The leading zero means an octal (base-8) literal, not a decimal (base-10) literal.

LocalDate.of( 1980 , 1 , 1 ) 

IDEs

Most Java programmers write their code within an IDE such as IntelliJ, Eclipse, or NetBeans. For students, BlueJ. These tools will help you in drafting your class, and will point out deficiencies.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.