Unknown user
Unknown user

Reputation: 45301

' \ '-Invalid character constant?

I need to do this:

while (result2.charAt(j) != '\'){
    
}

I get an error saying: Invalid character constant.

Why? And how can I get over it?

Upvotes: 5

Views: 23071

Answers (7)

live-love
live-love

Reputation: 52366

Same error here, but using unicode character representation.

005C is backlash character. Need to escape it: "\u005C" .

Example:

str = str.replace("\\u005C", "'\\u005C'");

Upvotes: 0

ameyx
ameyx

Reputation: 403

I got this similar error in Eclipse for Android although for different situation, and I just figured out that in Java you cannot enclose a string (multi-character word) in single quotes. So you need to have like - "sampleword" strings enclosed in double quotes rather than single quote to get rid of such error thought I could just share it here for others to refer..

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117587

you need an extra character '\'

" " " == " \" "


" \ " == " \\ "

Upvotes: 0

SquidScareMe
SquidScareMe

Reputation: 3218

Looks like you need to escape the backslash. Try

while (result2.charAt(j)!='\\'){

    }

Upvotes: 2

user628985
user628985

Reputation:

You need to escape it I think,

So you need to do

while(results2.charAt(j)!='\\')
{
}

I think that's the solution I think

Upvotes: 0

taskinoor
taskinoor

Reputation: 46027

Use '\\'. It's because backslash is used in escape sequence like '\n'. With a single \ the compiler have no way to know.

Upvotes: 3

Asaph
Asaph

Reputation: 162781

The backslash is a special character and it needs to be escaped with another backslash. Like this:

while (result2.charAt(j)!='\\'){

}

Upvotes: 9

Related Questions