hi people,

i have encountered this error message "unclosed character literal" while compiling the below program using bluej. I wonder what is wrong with the code,do enlighten me. Thanks!!

 public class Address
{
    private String street;
    private String city;
    private String zip;
    private String state;

    //Default constructor
    public Address ( )
    {
    }

    //Create address with street, city, state, and zip
    public Address ( String street, String city, String state, String zip )
    {
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }

    //Getter method for street
    public String getStreet ( )
    {
        return street;
    }

    //Setter method for street
    public void setStreet ( String street )
    {
        this.street = street;
    }

    //Getter method for city
    public String getCity ( )
    {
        return city;
    }

    //Setter method for city
    public void setCity ( String city )
    {
        this.city = city;
    }

    //Getter method for state
    public String getState ( )
    {
        return state;
    }

    //Setter method for state
    public void setState ( String state )
    {
        this.state = state;
    }

    //Getter method for zip
    public String getZip ( )
    {
        return zip;
    }

    //Setter method for zip
    public void setZip ( String zip )
    {
        this.zip = zip;
    }

    //Get full address
    public String getFullAddress ( )
    {
        return street + ' \n ' + city + " , " + state + ' ' + zip +  ' \n ';
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You can't enclose string definitions with single-quotes in Java. You must use double-quotes.

Please enclose your code in CODE tags.

Edited 3 Years Ago by happygeek: fixed formatting

The compiler will tell you what line to look for the error. Look there and apply your knowledge of what a character literal is and you'll see where you've tried to use something as a character literal that isn't.

hi,

i think i missed out something, the error actually points to the last statement code, and can i know what is the meaning of character literal?

thanks and rgds,
tris

It points to the last segment of code because that's where the error is.

A character literal just means a single character. You can use single-quotes to hold one character but not multiple. Check your code and make sure that you did not put more than one character between single-quotes.

thanks chrisbliss18 and all,

I have figured out the error, INSTEAD of using ' \n ' i SHOULD have used "\n" because there are two characters enclosed.

rgds,
tris

no, you are wrong. '\n' is a character, but you wrote '\n ' (note the extra space).
Learn your special characters.

hi jwenting,

thanks for pointing my error, i have changed from ' /n ' to '/n' and i can compile the codr. but i have a question, i can still compile the code with " /n " but what is the difference? from my gut feeling, i think when i run the whole program, i will have different output eg.

" /n " will give me a character while '/n' will give me a spacing

thanks for your advice and patience

rgds,
tris

'\n' will give you a newline character, "\n " will give you a newline character and a space, so your next line will start with an empty space.

This article has been dead for over six months. Start a new discussion instead.