Stray semicolon yes, but || no
As Answer by Spectric correctly says, you have stray semicolon to fix.
But the instruction to use || is incorrect.
Here is your code, corrected.
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
//System.out.println( "c = " + c );
if ( ( c != ' ' ) && ( c != ',' ) && ( c != '.' ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
See this code run live at IdeOne.com.
21
I would have written such code by using a List of forbidden characters rather than a series of equality tests.
String input = "Listen, Mr. Jones, calm down.";
List < String > nonLetters = List.of( " " , "," , "." );
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
System.out.println( "c = " + c );
if ( ! nonLetters.contains( String.valueOf( c ) ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
Even better: Use code points rather than char type.
Code points, not char
The char type is obsolete in Java. That type is a relic, unable to represent even half of the characters defined in Unicode.
Instead, work with code point number to represent each character. Unicode assigns one integer number to each of its 143,859 characters. The code points range from zero to over a million, most being unassigned.
The String class can return an IntStream of the code point number for each character. Convert that IntStream to an array of int numbers. Loop the array. For each code point number, ask if the character Unicode assigns to that number is considered a letter or not. If a letter, increment our count.
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
int[] codePoints = input.codePoints().toArray();
for ( int codePoint : codePoints )
{
if ( Character.isLetter( codePoint ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
21
Stream
We can even go so far as to make a one-liner using Java stream and lambda features.
long countLetters =
"Listen, Mr. Jones, calm down."
.codePoints()
.filter(
( int codePoint ) -> Character.isLetter( codePoint )
)
.count()
;
21