0

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 ]); 
            }
        } 
    }
}
2
  • 6
    is not a valid quote. Try with '. Commented May 10, 2015 at 14:54
  • I'm guessing that you copy pasted from some site, and most probably from some word editor, change ‘S’ to 'S' (‘ and ’ are not valid) Commented May 10, 2015 at 14:56

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ] ) ; 
        } 
    } 
        }
    }

Comments

0

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]);
                }
            }
        }
    }
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.