kkassie07
kkassie07

Reputation: 1

Java Syntax error?

So I'm sure I'm missing something obvious, I'm extremely new to programming and my school is online so I'm on my own. Based on my book, this code should work? I keep getting the error

"Syntax Error on tokens, Expression expected instead" //line 11

if (cities [ i ][ j ].charAt (0) =='S')

the "S" is bolded because Eclipse is showing the error there

If someone could point me in the right direction, I would be most appreciative! Thanks!

public class week6{
    public static void main (String [] args){
        for(int i = 0; i < cities.length; i++){ 
            for (int j = 0; j < cities [ i ] .length; j++){ 
                if(cities[ i ][ j ].charAt(0) == ‘S’) 
                    System.out.println(cities [ i ][ j ]); 
            }
        } 
    }
}

Upvotes: 0

Views: 1325

Answers (3)

DoubleMa
DoubleMa

Reputation: 368

Like other has mentioned use ' instead of and .

Or use if (cities[i][j].startsWith("S")) instead of if (cities[i][j].charAt(0) == 'S').

public class week6 {

    private static String[][] cities = {{"somecity1", "Somecity2"}, {"somecity3", "Somecity4"}};

    public static void main(String[] args) {
        for (int i = 0; i < cities.length; i++) {
            for (int j = 0; j < cities[i].length; j++) {
                if (cities[i][j].startsWith("S")) { //or  if (cities[i][j].charAt(0) == 'S') 
                    System.out.println(cities[i][j]);
                }
            }
        }
    }
}  

Upvotes: 0

destraaaa
destraaaa

Reputation: 96

Try changing your aphostrophe with ' So it would be

public class week6

    {
        public static void main (String[] args)
        {
        for ( int i = 0; i < cities.length; i++ ) 
    { 
        for ( int j = 0; j < cities [ i ] .length; j++ ) 
        { 
            if ( cities [ i ][ j ].charAt ( 0 ) == 'S' ) 
            System .out.println ( cities [ i ][ j ] ) ; 
        } 
    } 
        }
    }

Upvotes: 0

Spikatrix
Spikatrix

Reputation: 20244

Replace

if ( cities [ i ][ j ].charAt ( 0 ) == ‘S’ ) 

with

if ( cities [ i ][ j ].charAt ( 0 ) == 'S' )

and are different from '. The compiler complains because it doesn't recognize the curly apostrophes. Use normal apostrophes to fix the issue.

Upvotes: 2

Related Questions