Comp Sci Working with Character Literals in Java: Handling the Apostrophe"

  • Thread starter Thread starter K29
  • Start date Start date
Click For Summary
To handle an apostrophe as a character literal in Java, you can use the escape sequence '\''. This allows you to compare a character variable with an apostrophe without syntax errors. For example, using if (Word.charAt(0) == '\'') will work correctly. NetBeans may restrict direct input of the apostrophe, but using the escape sequence resolves this issue. Properly escaping characters is essential for accurate string manipulation in Java.
K29
Messages
103
Reaction score
0
If I want to work with a character literal, and it is the apostrophe, how do I do this?

For example:

Code:
if(Word.charAt(0)=='a'){
   \\do something
}else{
if(Word.charAt(0)=='''){
   \\do something else
}

Now, Word.charAt(0)==''' isn't allowed. In fact netbeans won't even let me type it. How do I overcome this?

Thanks in advance
 
Physics news on Phys.org
I would try '\''.

But I can be wrong.
 
That worked. Thank you.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K