[Solved] Unclosed String Literal Error

In this post, I will be sharing how to fix unclosed string literal error in Java. As the name suggests, this error occurs when the string literal ends without quotation marks. A String class object should be enclosed in double-quotes in Java, in case you miss them you will get this error. Unclosed String Literal error is a compile-time error. As always, first, we will produce the unclosed string literal error before moving on to the solution.

Read Also: Print quotation marks in the java

[Fixed] Unclosed String Literal Error

Example 1: Producing the error by having string literal that does not end with quotes

We can easily produce this error by using a string literal that does not end with quotes as shown below:

 public class UnclosedStringLiteral {
    public static void main(String args[]) {
        String str = "Love Yourself;
        System.out.println(str);
    }
}


Output:
UnclosedStringLiteral.java:3: error: unclosed string literal
               String str = "Love Yourself;
                                    ^

1 error

Explanation:

The cause of this error is due to the missing end quotes of string literal as shown above.

Solution:

The above compilation error can be resolved by providing the end quotes of string literal as shown below:

 public class UnclosedStringLiteral {
    public static void main(String args[]) {
        String str = "Love Yourself";
        System.out.println(str);
    }
}


Output:
Love Yourself


Example 2: Producing the error when string literal extends beyond a line

We can easily produce this error when string literal extends beyond a line i.e multiline string as shown below:

 public class UnclosedStringLiteral2 {
    public static void main(String args[]) {
        String str = "Be in Present
                        +   Alive is Awesome";
        System.out.println(str);
    }
}


Output:
UnclosedStringLiteral2.java:3: error: unclosed string literal
               String str = "Be in Present
                                   ^
UnclosedStringLiteral2.java:4: error: ';' expected
                                          + Alive is Awesome";
                                                         ^
UnclosedStringLiteral2.java:4: error: unclosed string literal
                                          + Alive is Awesome";
                                                                          ^
UnclosedStringLiteral2.java:4: error: not a statement
                                          + Alive is Awesome";
                                                           ^
4 errors


Explanation:

The cause of this error is due to the missing end and start quotes of the multiline string literal separated by + operator as shown above.

Solution:

The above compilation error can be resolved by providing the end and start quotes of multiline string literal separated by + operator as shown below:

 public class UnclosedStringLiteral2 {
    public static void main(String args[]) {
        String str = "Be in Present"
                        +   "Alive is Awesome";
        System.out.println(str);
    }
}


Output:
Be in PresentAlive is Awesome


Example 3: Producing the error when the part of the string literal is not escaped with a backslash("\")

We can easily produce this error when the part of the string literal is not escaped with a backslash("\") as shown below:

 public class UnclosedStringLiteral3 {
    public static void main(String args[]) {
        String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
        System.out.println(str);
    }
}


Output:
UnclosedStringLiteral3.java:3: error: ';' expected
               String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
                                                                                     ^
UnclosedStringLiteral3.java:3: error: ';' expected
               String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
                                                                                             ^
UnclosedStringLiteral3.java:3: error: not a statement
               String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
                                                                                              ^
UnclosedStringLiteral3.java:3: error: ';' expected
               String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
                                                                                                           ^
UnclosedStringLiteral3.java:3: error: unclosed string literal
               String str = "Alive is Awesome, "+"\" + "Be in Present, " + "Love Yourself" ;
                                                                                                                                             ^
5 errors


Explanation:

The cause of this error is due to the part of the string literal is not escaped with ("\") backslash character as shown above.

Solution:

The above compilation error can be resolved by escaping the backslash character as shown below:

 public class UnclosedStringLiteral3 {
    public static void main(String args[]) {
        String str = "Alive is Awesome, "+"\\" + "Be in Present, " + "Love Yourself" ;
        System.out.println(str);
    }
}


Output:
Alive is Awesome, \Be in Present, Love Yourself


That's all for today, please mention in the comments in case you are still facing the error unclosed string literal error in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry